如何在匹配后停止用户输入



所以,我终于开始做这项工作了,但我似乎无法阻止它请求用户输入。基本上,我想让代码做的是从用户那里获得一个数字,如果它匹配,那么继续打印每个";卷号(number(为(number("如果第一个输入没有匹配,那么它可能会再次询问(最多三次,直到匹配为止(。如果匹配,则用户获胜,如果不匹配,则计算机获胜。我认为范围(3(可能是我的问题,但我只是不确定,因为我确实尝试删除它

dashes = 65
dashes_count = 65 * "-"
print(f'You have three rolls of the dice to match a number you select.')
print(f'Good Luck!')
print(dashes_count)
import random
die = 0
roll = 0
def dice_roll():
    dieroll = random.randint(1, 6) + random.randint(1, 6)
    return dieroll
for die in range(3):
    die1 = int(input(f'Choose a number between 2 and 12: '))
    die2 = int(input(f'Choose a number between 2 and 12: '))
    die3 = int(input(f'Choose a number between 2 and 12: '))
    roll1 = dice_roll()
    roll2 = dice_roll()
    roll3 = dice_roll()
    if die1 == roll1:
        break
        print(f'Roll # 1 was {roll1}')
        print(f'Roll # 2 was {roll2}')
        print(f'Roll # 3 was {roll3}')
        print(f'You Win! - Thanks for playing!')
    if die2 == roll2:
        break
        print(f'Roll # 1 was {roll1}')
        print(f'Roll # 2 was {roll2}')
        print(f'Roll # 3 was {roll3}')
        print(f'You Win! - Thanks for playing!')
    if die3 == roll3:
        break
        print(f'Roll # 1 was {roll1}')
        print(f'Roll # 2 was {roll2}')
        print(f'Roll # 3 was {roll3}')
        print(f'You Win! - Thanks for playing!')
    else:
        print(f'Roll # 1 was {roll1}')
        print(f'Roll # 2 was {roll2}')
        print(f'Roll # 3 was {roll3}')
        print(f'You Lose! - Thanks for playing!')

我正在寻找一个像这样工作的输出:

Choose a number between 2 and 12: 3 # user input
roll # 1 was 5
roll # 2 was 3 # the matching number
roll # 3 was 11
You Win! - Thanks for playing!
# where the user input matches 1 or all rolls and prints all roll results.
# otherwise 
Choose a number between 2 and 12: g # not valid / not a match
Choose a number between 2 and 12: 5 # the second try
roll # 1 was 7
roll # 2 was 12
roll # 3 was 5 # the matching number
You Win! - Thanks for playing
# " Choose a number between 2 and 12: " is limited to 3 tries only
# if all three tries receive in valid / unmatches numbers, print "You lose!"

我不太确定你在寻找什么行为。也许就是这样:

import random
dashes = 65
dashes_count = 65 * "-"
print(f'You have three rolls of the dice to match a number you select.')
print(f'Good Luck!')
print(dashes_count)

die = 0
roll = 0
def dice_roll():
    dieroll = random.randint(1, 6) + random.randint(1, 6)
    return dieroll
for roll_number in range(3): # Do it 3 times. (3 tries for the user)
    die = int(input(f'Choose a number between 2 and 12: '))
    roll = dice_roll()
    print(f'Roll # {roll_number} was {roll}') # Tell the user what was rolled
    if die == roll:                           # Check if the user wins
      print(f'You win!')
      exit()                                  # Exit the game if he won
    else:
      print('Thats not it! Try again!')       # Tell him that he lost
print('You lost, thanks for playing!')        # If he arrives here, he has 
                                              # lost all 3 times and the programm is finished executing

输出:

You have three rolls of the dice to match a number you select.
Good Luck!
-----------------------------------------------------------------
Choose a number between 2 and 12: 5
Roll # 0 was 11
Thats not it! Try again!
Choose a number between 2 and 12: 6
Roll # 1 was 8
Thats not it! Try again!
Choose a number between 2 and 12: 3
Roll # 2 was 8
Thats not it! Try again!
You lost, thanks for playing!
You have three rolls of the dice to match a number you select.
Good Luck!
-----------------------------------------------------------------
Choose a number between 2 and 12: 5
Roll # 0 was 9
Thats not it! Try again!
Choose a number between 2 and 12: 5
Roll # 1 was 5
You win!

请注意,我们不必保存3个不同的卷,而是可以使用每轮一个并覆盖它,因为上一个滚动对我们正在玩的本轮并不重要。

这应该是您的代码:

In [1899]: die = 0
      ...: roll = 0
      ...: 
      ...: def dice_roll():
      ...:     dieroll = random.randint(1, 6) + random.randint(1, 6)
      ...:     return dieroll
      ...: 
      ...: for die in range(3):
      ...:     die1 = int(input(f'Choose a number between 2 and 12: '))
      ...:     roll1 = dice_roll()
      ...:     if die1 == roll1:
      ...:         print(f'Roll # 1 was {roll1}')
      ...:         print(f'You Win! - Thanks for playing!')
      ...:     else:
      ...:         print(f'Roll # 1 was {roll1}')
      ...:         print(f'You Lose! - Thanks for playing!')
      ...: 
Choose a number between 2 and 12: 3
Roll # 1 was 6
You Lose! - Thanks for playing!
Choose a number between 2 and 12: 4
Roll # 1 was 5
You Lose! - Thanks for playing!
Choose a number between 2 and 12: 5
Roll # 1 was 9
You Lose! - Thanks for playing!

我相信多用一个for cycle:更容易解决这个问题

dashes = 65
dashes_count = 65 * "-"
print(f'You have three rolls of the dice to match a number you select.')
print(f'Good Luck!')
print(dashes_count)
import random

def dice_roll():
    dieroll = random.randint(1, 6) + random.randint(1, 6)
    return dieroll

def main():
    won = False
    for i in range(3):
        if (won):
            print(f'You Win! - Thanks for playing!')
            break
        randRol = dice_roll()
        die = int(input(f'Choose a number between 2 and 12: '))
        for j in range(3):
            randRol = dice_roll()
            print(f'The ', j+1, ' roll was ', randRol)
            if(die == randRol):
                
                won = True
                break
    if (not won):
        print(f'You Lose! - Thanks for playing!')

main()
            
                        
        
        
                    
    

如果我们使用";中断";它只会从一个for循环中逃脱,所以我使用了一个bool变量来解决这个问题。程序停止时;赢得";变为True。我希望这能有所帮助。

输出:

You have three rolls of the dice to match a number you select.
Good Luck!
-----------------------------------------------------------------
Choose a number between 2 and 12: 7
The  1  roll was  3
The  2  roll was  6
The  3  roll was  6
Choose a number between 2 and 12: 7
The  1  roll was  9
The  2  roll was  6
The  3  roll was  6
Choose a number between 2 and 12: 7
The  1  roll was  6
The  2  roll was  12
The  3  roll was  10
You Lose! - Thanks for playing!
>>> 

相关内容

  • 没有找到相关文章

最新更新