python - 如何让这个程序更详细,并在我玩完后告诉我胜率?
问题描述
到目前为止,这个掷骰子游戏在 python 中运行,但我需要在你玩完之后添加胜率。另外,我如何让玩家响应必须是“是”或“否”才能运行?无论如何它都会运行。有没有办法在这个程序中替换“中断”?
import random
first_roll = 0
def play():
yesOrno = str(input('Would you like to play? '))
yesOrno = yesOrno[0].lower()
while yesOrno == 'y':
throw_1 = random.randint(1,6)
throw_2 = random.randint(1,6)
total = throw_1 + throw_2
if total == (2,12):
yesOrno = str(input('You lost! Would you like to play again?'))
yesOrno = yesOrno[0].lower()
elif total == (7,11):
yesOrno == str(input('You won! Would you like to play again?'))
yesOrno = yesOrno[0].lower()
else:
first_roll == total
while yesOrno == 'y':
throw_1 = random.randint(1,6)
throw_2 = random.randint(1,6)
finalRoll = throw_1 + throw_2
print('You rolled a',total)
if total == 2 or 3 or 7 or 11 or 12:
yesOrno = str(input('You lost! Would you like to play again?'))
yesOrno = yesOrno[0].lower()
break
elif total == first_roll:
yesOrno = str(input('You won! Would you like to play again?'))
yesOrno = yesOrno[0].lower()
break
else:
yesOrno == 'y'
print('Thanks for playing!')
play()
解决方案
我修复并简化了您的一些代码。我为重复性任务创建了 2 个助手:
import random
def askPlayAgain(text=""):
"""Ask for y or n, loop until given. Maybe add 'text' before the message."""
t = ""
while t not in {"y","n"}:
t = input("\n" + text + 'Would you like to play? [y/n]').strip()[0].lower()
return t
def getSumDices(n=2,sides=6):
"""Get sum of 'n' random dices with 'sides' sides"""
return sum(random.choices(range(1,sides+1),k=n)) # range(1,7) = 1,2,3,4,5,6
并稍微修改了播放逻辑:
def play():
first_roll = 0
win = 0
lost = 0
yesOrno = askPlayAgain()
while yesOrno == 'y':
# dont need the individual dices - just the sum
# you can get both rolls at once
total = getSumDices()
print('You rolled a', total)
if total in {2,12}:
lost += 1
yesOrno = askPlayAgain("You lost! ")
elif total in {7,11}:
win += 1
yesOrno == askPlayAgain("You won! ")
else:
# remember last rounds result
first_roll = total
while True:
total = getSumDices()
print('You rolled a', total)
# this is kinda unfair, if you had 3 in your first round
# you cant win the second round at all ...
if total in {2,3,7,11,12}:
lost += 1
yesOrno = askPlayAgain("You lost! ")
break
elif total == first_roll:
win += 1
yesOrno = askPlayAgain("You won! ")
break
print('Thanks for playing!')
print('{} wins vs {} lost'.format(win,lost))
play()
输出:
Would you like to play? [y/n]y
You rolled a 10
You rolled a 12
You lost! Would you like to play? [y/n]y
You rolled a 9
You rolled a 7
You lost! Would you like to play? [y/n]y
You rolled a 7
You won! Would you like to play? [y/n]y
You rolled a 6
You rolled a 10
You rolled a 3
You lost! Would you like to play? [y/n]y
You rolled a 8
You rolled a 9
You rolled a 11
You lost! Would you like to play? [y/n]n
Thanks for playing!
1 wins vs 4 lost
推荐阅读
- android - TextView 呈现丑陋的子弹
- 标签
- laravel - 在 laravel 5.4 中调整图像大小的 API
- java - 如何在 JavaFx 中显示按钮中的所有文本
- php - Wordpress 分页数
- reactjs - admin-on-rest 项目怎么会有用户注册页面?
- reactjs - Firebase 上的 React 应用程序:未显示新部署(浏览器中缓存的旧部署)
- hive - 将 Parquet 文件编辑为二进制文件
- javascript - AWS Dynamodb 没有通过节点 js 同步获取数据
- regex - 如果之间没有数字,则获取发生
- google-sheets - Google 工作表 IF 匹配两个条件,然后复制四个不同的列