Python Homework 6

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

1)

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

final = totalgas / totalkms


print("The liters of gas consumed per kilometer for the whole trip is",
final)

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()

You might also like