Python Homework 6
Python Homework 6
def main():
odometer = float(input("What is your starting odometer reading?"))
totalgas = 0
totalkms = 0
while True:
kms = (input("What is the odometer reading right now?"))
if kms == "":
break
kms = float(kms)
gas = float(input("How much gas did you consume?"))
tkms = kms - odometer
print("You have used", tkms, "kilometers per liter for this leg")
totalkms += tkms
totalgas += gas
main()
2)
# This program allows the user to generate a random number
def main():
import random
random = random.randint(0, 30)
counter = 0
for i in range(random):
while True:
try:
Usersguess = int(input("Guess a number:"))
counter += 1
if Usersguess == random:
print("You guessed it in:", counter, "times!")
break
elif Usersguess > random:
print("Your guess is too high fam")
elif Usersguess < random:
print("Your guess is too low fam")
else:
print("Number is not in range given")
except ValueError:
print("This number is not supported by this program")
while True:
x = input("Would you like to exit the program? If so, write yes.")
if x == "yes":
return
main()
3)
# This program allows the user to generate a random number
def main():
import random
random = random.randint(0, 30)
counter = 0
for i in range(random):
while True:
try:
Usersguess = int(input("Guess a number:"))
counter += 1
if Usersguess == random:
print("You guessed it in:", counter, "times!")
break
elif Usersguess > random:
print("Your guess is too high fam")
elif Usersguess < random:
print("Your guess is too low fam")
else:
print("Number is not in range given")
except ValueError:
print("This number is not supported by this program")
while True:
x = input("Would you like to exit the program? If so, write q or
Q.")
if x == "q" or "Q":
return
main()
4)
# This program allows the user to generate a random number
def main():
import random
random = random.randint(0, 30)
counter = 0
for i in range(random):
while True:
try:
Usersguess = int(input("Guess a number:"))
counter += 1
if Usersguess == random:
print("You guessed it in:", counter, "times!")
break
elif Usersguess > random:
print("Your guess is too high fam")
elif Usersguess < random:
print("Your guess is too low fam")
else:
print("Number is not in range given")
except ValueError:
print("This number is not supported by this program")
while True:
x = input("Would you like to exit the program? If so, write yes")
if x == "yes":
break
main()