首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python 3掷骰子模拟器的问题

Python 3掷骰子模拟器的问题
EN

Stack Overflow用户
提问于 2019-01-07 04:58:21
回答 1查看 61关注 0票数 0

所以我在这段本应模拟掷骰子模拟器的代码中遇到了一些问题,但无论我怎么尝试,我都无法再次跳出while循环。

代码语言:javascript
复制
import random

print('-------------------------------------------------------')
print('       WELCOME TO THE DICE ROLLING SIMULATOR')
print('-------------------------------------------------------')

while True:
    randomNumber = str(random.randint(1, 6))
    rollAgain = input('would you like to roll the dice? yes or no?')
    if rollAgain == 'yes' or ' yes':
        print('Rolling the dice')
        print('the dice landed on the number: ' + randomNumber)
   elif rollAgain == 'no' or ' no':
        quit()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-07 05:03:42

每次都需要根据值检查变量。在队伍中

代码语言:javascript
复制
if rollAgain == 'yes' or ' yes':

Python每次都会将' yes'计算为true。您还需要将其与rollAgain进行比较。

下面是固定的代码:

代码语言:javascript
复制
import random

print('-------------------------------------------------------')
print('       WELCOME TO THE DICE ROLLING SIMULATOR')
print('-------------------------------------------------------')

while True:
    randomNumber = str(random.randint(1, 6))
    rollAgain = input('would you like to roll the dice? yes or no?')
    if rollAgain == 'yes' or rollAgain ==  ' yes':
        print('Rolling the dice')
        print('the dice landed on the number: ' + randomNumber)
    elif rollAgain == 'no' or rollAgain == ' no':
        quit()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54065887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档