2017-08-13 23 views
0

私はPYTHONで小さなランダム推測ゲームを作ろうとしていますが、ユーザーがプレイした後にはうまくいきますが、「y」が入力された場合に再度プレイすることができます。だから、コードの先頭に戻ります。私はそのループを理解していますが、動作させる方法を理解できません。 ご迷惑をおかけして申し訳ございません。ランダム推測ゲーム

import random 



bet = input("How much would you like to bet?") 
print ("You are betting €",bet,) 

win = int(bet) * int(2) 

print('Pick either 1 or 0') 
number = random.randint(0,1) 
guess = 0 
while number < 2: 

    guess_number = int(input('Enter a number:')) 


    if guess_number == number: 
     print('Your guess is correct the number is',number) 
     print("you have won €",win,) 
     break 
    if guess_number >=2: 
     print ("Number too high") 
    else: 
     print("you are wrong, you have lost €",bet,) 
     break 

play_again = input("Play Again? y/n") 
y = bet 
n = print("bye") 
if play_again == y: 
    bet 

答えて

1

whileループに(離れimport randomから)全体コードを置き。

while True: 
    bet = input("How much would you like to bet?") 
    print ("You are betting €",bet,) 

    win = int(bet) * int(2) 

    print('Pick either 1 or 0') 
    number = random.randint(0,1) 
    guess = 0 
    while number < 2: 

     guess_number = int(input('Enter a number:')) 


     if guess_number == number: 
      print('Your guess is correct the number is',number) 
      print("you have won €",win,) 
      break 
     if guess_number >=2: 
      print ("Number too high") 
     else: 
      print("you are wrong, you have lost €",bet,) 
      break 

    play_again = input("Play Again? y/n") 
    if play_again.lower() == "y": 
     continue 
    else: 
     break 
0

またように機能してあなたのコードを置くことができます:

import random 

def game(): 
    bet = input("How much would you like to bet?") 
    print ("You are betting €",bet,) 

    win = int(bet) * int(2) 

    print('Pick either 1 or 0') 
    number = random.randint(0,1) 
    guess = 0 
    while number < 2: 

     guess_number = int(input('Enter a number:')) 


     if guess_number == number: 
      print('Your guess is correct the number is',number) 
      print("you have won €",win,) 
      break 
     if guess_number >=2: 
      print ("Number too high") 
     else: 
      print("you are wrong, you have lost €",bet,) 
      break 
    play_again = input("Play Again? y/n: ") 
    if play_again =='y': 
     game() 
    if play_again =='n': 
     n = print("bye") 
game()