Python中文
首页
教程
问答
标签
搜索
登录
注册
初级python游戏:垃圾,whileloop问题
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我是编程新手,我的任务是做一个垃圾游戏。游戏一直在进行,直到你被要求再次玩时回答“否”。一旦你输入“否”,它应该显示你赢了和输了多少次。这样做效果很好(不记分):</p> <pre><code>import random def main(): playGame = input("Would you like to play Craps? (Enter yes or no): ") while playGame == 'yes': roll = input("Press Enter to roll the dice") rollDice1 = random.randint(1, 6) rollDice2 = random.randint(1, 6) print("You got a", rollDice1, "and a", rollDice2) rolledDice = rollDice1 + rollDice2 print("you rolled a", rolledDice) if rolledDice == 7 or rolledDice == 11: print("IT'S YOUR LUCKY DAY! YOU WIN!") elif rolledDice == 2 or rolledDice == 3 or rolledDice == 12: print("YOU LOSE! BETTER LUCK NEXT TIME!") else: print("YOU NEITHER WIN NOR LOSE!") playGame = input("Try again? (Enter yes or no): ") if playGame == "no": print("Place holder") main() </code></pre> <p>当我试图记分时,当你赢或输的时候,它不会循环。(尽管在你不赢也不输的情况下,它仍然会循环):</p> <pre><code>import random def main(): wins = 0 losses = 0 playGame = input("Would you like to play Craps? (Enter yes or no): ") while playGame == 'yes': roll = input("Press Enter to roll the dice") rollDice1 = random.randint(1, 6) rollDice2 = random.randint(1, 6) print("You got a", rollDice1, "and a", rollDice2) rolledDice = rollDice1 + rollDice2 print("you rolled a", rolledDice) if rolledDice == 7 or rolledDice == 11: print("IT'S YOUR LUCKY DAY! YOU WIN!") wins = wins + 1 return wins elif rolledDice == 2 or rolledDice == 3 or rolledDice == 12: print("YOU LOSE! BETTER LUCK NEXT TIME!") losses = losses + 1 return losses else: print("YOU NEITHER WIN NOR LOSE!") playGame = input("Try again? (Enter yes or no): ") if playGame == "no": print("Wins: ", wins) print("Losses: ", losses) main() </code></pre> <p>我非常感谢您的帮助和建议。就像我说的,我是新手,所以请试着用一种简单的方式来解释什么是错的,我应该做什么</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>不要使用<code>return</code>,而是使用<code>yield</code>。Return结束函数以给出结果,而<code>yield</code>继续函数</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何找到类似于How'matplotlib.pyplot.gcf()`works?
4 回答
如何找到类字段的定义?
9 回答
如何找到精灵在团队中的位置?
2 回答
如何找到素数,但有错误。我找不到你
9 回答
如何找到素数(Python)
9 回答
如何找到索引i右侧的不同值
10 回答
如何找到索引Numpy数组时将折叠哪些轴?
10 回答
如何找到索引中的值,在列表中增加值?
4 回答
如何找到纬度/经度/高度点之间的三维距离?
3 回答
如何找到线和numpy meshgrid生成的曲面之间的交点?
4 回答
如何找到线段上距任意点最近的点?
7 回答
如何找到组中所有可能的子组
5 回答
如何找到组内值之间的最小差异
5 回答
如何找到经过训练的朴素贝叶斯分类器用于决策的单词?
7 回答
如何找到给selenium webdriver对象的文件夹名?
6 回答
如何找到给出最佳分数的列车测试分割的最佳随机状态值?
6 回答
如何找到给定Python发行版提供的模块?
3 回答
如何找到给定subversion工作副本的根文件夹
9 回答
如何找到给定一维阵列中的所有峰值?
3 回答
如何找到给定列表中的字符串组合,这些字符串加起来就是某个字符串(没有外部库)
10 回答