0
#The program is as below.
該程序允許用戶嘗試兩次猜測兩個彩票號碼。 如果用戶正確猜測我的號碼,用戶將得到100美元,並有一次玩的機會。如果在第二次機會中用戶再次猜測一個數字,則用戶無法獲得更多。擺脫一些輸出
import random
guessed=False
attempts=2
while attempts > 0 and not guessed:
lottery1= random.randint(0, 99)
lottery2= random.randint(45,109)
guess1 = int(input("Enter your first lottery pick : "))
guess2 = int(input("Enter your second lottery pick : "))
print("The lottery numbers are", lottery1, ',', lottery2)
if guess2==lottery2 or guess1==lottery1:
print("You recieve $100!, and a chance to play again")
attempts-=1
if (guess1 == lottery1 and guess2 == lottery2):
guessed=True
print("You got both numbers correct: you win $3,000")
else:
print("Sorry, no match")
輸出如下:
Enter your first lottery pick : 35
Enter your second lottery pick : 45
The lottery numbers are 35 , 78
You recieve $100!, and a chance to play again
Sorry, no match
Enter your first lottery pick : 35
Enter your second lottery pick : 45
The lottery numbers are 35 , 45
You recieve $100!, and a chance to play again
You got both numbers correct: you win $3,000
Sorry, no match
我想擺脫線的「你收到$ 100!和再玩一次機會」,當用戶正確,並在猜測這兩個數字第二次嘗試如果用戶猜測一個數字是正確的。我希望這是有道理的
嘗試在'if if(guess1 == lottery1 and guess2 == lottery2)''elif'語句中移動'if guess2 == lottery2或guess1 == lottery1' –
Thatnks Vitalii。這樣做可以擺脫「你收到100美元!並有機會再次播放」,以防用戶猜到兩個數字。如果第二次猜測錯誤,它不會擺脫「你收到100美元!並且有機會再次玩」。儘管我非常感謝你的幫助! –