2016-11-17 8 views
0
#The program is as below. 

このプログラムでは、2回の試行で2回の宝くじ番号を推測できます。 ユーザーがi番号を正しく推測すると、ユーザーは100ドルとなり、もう1回チャンスが得られます。 2度目のチャンスでユーザーがもう一度1つの数字を推測すると、ユーザーは何も取得しません。いくつかの出力を取り除く

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受け取る!と再びプレーするチャンス」ユーザーが正しくで両方の数値を推測する場合を取り除きたいです2番目の試行ユーザーが正しい番号を推測する場合。私は意味があることを願っています。

+0

if(guess1 == lottery1とguess2 == lottery2)の後の 'elif'文の' guess2 == lottery2 or guess1 == lottery1'を移動しようとします。 –

+0

Thatnks Vitalii。ユーザーが両方の数字を推測する場合には、「あなたは100ドルを受け取る!そしてもう一度遊ぶチャンス」を取り除く。もう一度推測が間違っていれば、 "あなたは100ドルを受け取る!そしてもう一度遊ぶチャンス"という行を取り除かない。私は本当にあなたの助けに感謝します! –

答えて

0

ここにあるコードスニペットのインデントは、IDEにあるものと同じです。あなたが見ることができるように、elseステートメントは正しくインデントされていません。だから、最初にあなたはあなたの宝くじ番号のリストを使用して、あなたのコードがより柔軟性がある方法で、このように多くの一致を確認するユーザーの推測を確認することをお勧めしますマッチを確認する必要があります。両方の数字が一致する場合は、少なくとも1つが一致するかどうかテストしない場合は表示されず、Sorry, no matchというメッセージが表示されます。 だから、コードは次のようになります。

matches = 0 
lottery = [random.randint(0, 99), random.randint(45,109)] 
guesses = [guess1, guess2] 
for guess in guesses: 
    if guess in lottery: 
     matches+=1 
# so now we know how many matches we have 
# matches might be more than length of numbers in case you have the the same numbers in lottery 
if matches >= len(lottery): 
    guessed=True 
    print("You got both numbers correct: you win $3,000") 
elif matches == 1: 
    print("You receive $100!, and a chance to play again") 
else: 
    print("Sorry, no match") 
    attempts-=1 

それは役に立ちました願っています!

関連する問題