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

Python中的掷骰子模拟器
EN

Stack Overflow用户
提问于 2017-05-17 01:58:51
回答 5查看 92K关注 0票数 0

我想写一个掷骰子的程序。这就是我所拥有的:

代码语言:javascript
复制
import random
print("You rolled",random.randint(1,6))

我也希望能够做这样的事情:

代码语言:javascript
复制
print("Do you want to roll again? Y/N")

如果我按下Y,它会再次滚动,如果我按下N,就会退出应用程序。提前感谢!

EN

回答 5

Stack Overflow用户

发布于 2018-01-08 00:52:56

代码语言:javascript
复制
import random
min = 1
max = 6

roll_again = "yes"
 roll_again = raw_input

while roll_again == "yes" or roll_again == "y":
    print ("Rolling the dices...")
    print ("The values are....")
    print (random.randint(min, max))
    print (random.randint(min, max))

    roll_again = raw_input("Roll the dices again?")
票数 -1
EN

Stack Overflow用户

发布于 2018-03-17 19:03:43

这是针对python 3的。

代码语言:javascript
复制
import random
repeat="Y"
while repeat == "Y":
   print("Rolling the dice")
   print(random.randint(1,6))

repeat =input("Do you wanna roll again Y/N?")
if repeat=="Y":
    continue
票数 -1
EN

Stack Overflow用户

发布于 2018-04-19 02:02:20

我三天前才开始学习Python,但这是我为Python3想出来的,它是有效的:

代码语言:javascript
复制
import random

question = input('Would you like to roll the dice [y/n]?\n')

while question != 'n':
    if question == 'y':
        die1 = random.randint(1, 6)
        die2 = random.randint(1, 6)
        print(die1, die2)
        question = input('Would you like to roll the dice [y/n]?\n')
    else:
        print('Invalid response. Please type "y" or "n".')
        question = input('Would you like to roll the dice [y/n]?\n')        

print('Good-bye!')
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44008489

复制
相关文章

相似问题

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