【问题标题】:How to stop user input once a match is made匹配后如何停止用户输入
【发布时间】:2020-10-24 14:15:08
【问题描述】:

所以,我终于开始着手这项工作,但我似乎无法阻止它要求用户输入。 基本上,我想要代码做的是从用户那里获取一个数字,如果它匹配,那么继续打印每个“roll # (number) was (number)”。如果第一个输入没有匹配,那么它可能会再次询问(最多 3 次直到匹配)。如果匹配成功,则用户获胜,否则,计算机获胜。我认为 range(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!"

【问题讨论】:

  • dashes_countdashes的名字好像是颠倒的,count并没有用来实际控制破折号的数量。最好不要打印很多破折号。

标签: python printing user-input python-3.8


【解决方案1】:

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

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 个不同的卷,而是可以使用 每轮一个并覆盖它,因为前一卷与我们正在玩的当前轮无关。

【讨论】:

  • 但是对于每一轮,都要求打印所有 3 卷。从 #1 到 #3 必须打印结果。对于每一轮,我需要“在 2 到 12 之间选择一个数字”在每次收到大于 12 或小于 2 的数字(限制为 3)时重复,然后打印结果。
  • @lou 我确信您已经拥有什么,并且通过给出的答案,您可以自己弄清楚。也许玩一下,如果您遇到问题,请提出另一个更具体的问题。
【解决方案2】:

这应该是您的代码:

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!

【讨论】:

  • 这很好用,但我需要“选择一个介于 2 到 12 之间的数字”来循环至少 3 次,如果它收到一个小于或大于 2 到 12 的数字或无效的东西,例如字母并打印卷#1 到#3。我在我的问题中添加了一个示例输出。
【解决方案3】:

我相信多一个for循环会更容易解决这个问题:

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()
            
                        
        
        
                    
    

如果我们使用“break”,它只会从其中一个 for 循环中逃脱,所以我使用了一个 bool 变量来解决问题。当“赢”变为真时,程序停止。 我希望这会有所帮助。

输出:

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!
>>> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-29
    • 2015-07-08
    • 2023-01-12
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多