python 3.x - Get rid of some output -
#the program below.
the program allows user try 2 times guess 2 lottery numbers. if user guesses number correctly,user gets $100 , 1 more chance play. if in second chance user guesses 1 number again, user gets nothing more.
import random guessed=false attempts=2 while attempts > 0 , not guessed: lottery1= random.randint(0, 99) lottery2= random.randint(45,109) guess1 = int(input("enter first lottery pick : ")) guess2 = int(input("enter second lottery pick : ")) print("the lottery numbers are", lottery1, ',', lottery2) if guess2==lottery2 or guess1==lottery1: print("you recieve $100!, , chance play again") attempts-=1 if (guess1 == lottery1 , guess2 == lottery2): guessed=true print("you got both numbers correct: win $3,000") else: print("sorry, no match")
the output below:
enter first lottery pick : 35 enter second lottery pick : 45 lottery numbers 35 , 78 recieve $100!, , chance play again sorry, no match enter first lottery pick : 35 enter second lottery pick : 45 lottery numbers 35 , 45 recieve $100!, , chance play again got both numbers correct: win $3,000 sorry, no match
i want rid of line " recieve $100!, , chance play again" when user guesses both numbers correctly , in second attempt if user guesses 1 number correct. hope makes sense
i assume indentation of code snippet have here same have in ide. can see else statement not indented correctly. first have check how many matches have, suggest use list of lottery numbers , check against user guesses see how many matches, way code more flexible. if both numbers matches, if not test if @ least 1 matches, if none of them show them sorry, no match
message. code should :
matches = 0 lottery = [random.randint(0, 99), random.randint(45,109)] guesses = [guess1, guess2] guess in guesses: if guess in lottery: matches+=1 # know how many matches have # matches might more length of numbers in case have the same numbers in lottery if matches >= len(lottery): guessed=true print("you got both numbers correct: win $3,000") elif matches == 1: print("you receive $100!, , chance play again") else: print("sorry, no match") attempts-=1
hope helpful!
Comments
Post a Comment