0% found this document useful (0 votes)
20 views

Python Homework 6

The document contains 4 code snippets that allow a user to guess a randomly generated number. The program tracks the number of guesses and provides feedback if the guess is too high, too low, or correct. The user can choose to exit the program after each round.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Python Homework 6

The document contains 4 code snippets that allow a user to guess a randomly generated number. The program tracks the number of guesses and provides feedback if the guess is too high, too low, or correct. The user can choose to exit the program after each round.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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