我如何改善有关while循环的python代码

克登

有人可以看看我的代码,并告诉我一些使我的代码更高效或更短的方法。我的程序基本上会生成1到6之间的2个数字并取它们的和。如果总和等于3,7,11,则程序会说“您赢了”。如果总和为奇数,则表示“您输了”。如果总和是偶数,则显示为“抽取”。最后,它显示获胜游戏的数量和获胜游戏的百分比。我该如何让它询问用户是否想再次发挥更高的效率呢(对python还是很新的)。谢谢

import random
random.seed(1234)

GamesPlayed=0
Won=0

print "DICE ROLLING GAME"
print

while True:
    #generates 2 numbers from 1 to 6
    num1=random.randint(1,6) 
    num2=random.randint(1,6) 

    total=num1+num2

    #This part checks to see if that total is equal to 3,7, or 11. it will say you win
    if total==3 or total==7 or total==11:
        print "I just rolled %d and %d." % (num1, num2)
        GamesPlayed+=1
        Won+=1
        print "You Win!"
        print 
        #next part ask user if they would like to play again
        user=raw_input("Would you like to try again (y/n): ") 
        if user=="N" or user=="n":
            break
        elif user=="Y" or user=="y":
            continue
    #next part checks to see if the two random numbers are odd numbers, if so, it displays "you lose"    
    elif total % 2==1:
        print "I just rolled %d and %d." % (num1, num2)
        print "Lose!"
        GamesPlayed+=1
        print 
        #ask if the user would want to go again
        user=raw_input("Would you like to try again (y/n): ") 
        if user=="N" or user=="n":
            break
        elif user=="Y" or user=="y":
            continue
    #If the total is an even number, it say "draw"
    elif total % 2==0:
        print "I just rolled %d and %d." % (num1, num2)
        print "Draw"
        GamesPlayed+=1
        print 
        user=raw_input("Would you like to try again (y/n): ") 
        if user=="N" or user=="n":
            break
        elif user=="Y" or user=="y":
            continue       

#displays how many games the user won out of the number of games they played, also displays the percentage of the amount they won
print "You won %d out of %d games, or %.0f%%." % (Won, GamesPlayed, (float(Won) / GamesPlayed) * 100)
简单的

您在其中重复相同的代码,if/elif但一次只能执行一次。

您可以使用lower(),然后不必与upper比较N您可以使用,strip()因为有时人们会在答案中加空格而看不到。

您可以使用if total in (3, 7, 11):
可以使用类似的ie。if user in ('n', 'no', 'quit'):

请参阅PEP 8-Python代码样式指南

  • 使用lower_case名称作为变量
  • 周围添加空格===+=,等
  • 在逗号后添加空格

代码:

import random
import time

random.seed(time.time())

games_played = 0
won = 0

print "DICE ROLLING GAME"
print

while True:
    games_played += 1

    num1 = random.randint(1, 6) 
    num2 = random.randint(1, 6) 

    total = num1 + num2

    print "I just rolled %d and %d (total: %d)." % (num1, num2, total)

    if total in (3, 7, 11):
        print "You Win!"
        won += 1
    elif total % 2 == 1:
        print "Lose!"
    #elif total % 2 == 0:
    else:
        print "Draw"

    print 

    answer = raw_input("Would you like to try again (y/n): ")
    answer = answer.strip().lower()

    if answer == "n":
        break

    print

print "You won %d out of %d games, or %.0f%%." % (won, games_played, (float(won) / games_played) * 100)

并使用一些随机值作为种子(即当前时间戳记),因为seed(1234)它总是给出相同的结果。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何改善有关堆排序的Python代码?

来自分类Dev

我的代码中与while循环和数组有关的问题

来自分类Dev

高效的循环代码?另外,有关“ for each”循环的查询

来自分类Dev

高效的循环代码?另外,有关“ for each”循环的查询

来自分类Dev

我想获得有关 Python 中延迟和循环的帮助

来自分类Dev

我需要有关此代码的帮助

来自分类Dev

为什么循环嵌套的顺序与python有关?

来自分类Dev

为什么循环嵌套的顺序与python有关?

来自分类Dev

与行号而不是代码有关的错误。如何解决?

来自分类Dev

如何打印有关代码执行的数据?

来自分类Dev

有关python代码的多语言关注?

来自分类Dev

有关转换为数组的Python代码

来自分类Dev

有关函数的代码中的问题(Python)

来自分类Dev

如何在我的 Python 代码中使用 while 循环

来自分类Dev

寻找有关如何在特定测试中改善常规性能的想法

来自分类Dev

我如何获得有关LoCo团队的更新?

来自分类Dev

如何使Redis通知我的服务有关事件

来自分类Dev

我如何获得有关LoCo团队的更新?

来自分类Dev

我如何检查有关可用性的过程?

来自分类Dev

elasticsearch-有关如何组织我的数据的提示

来自分类Dev

我在与元组有关的python中遇到问题

来自分类Dev

有关矩阵的循环实现问题

来自分类Dev

有关指针数组的C代码

来自分类Dev

有关交流代码输出的说明

来自分类Dev

Selenium,有关代码重构的问题

来自分类Dev

Python-如何获取有关SyntaxError的更多信息?

来自分类Dev

有关如何加快此python函数速度的建议?

来自分类Dev

Python-如何获取有关SyntaxError的更多信息?

来自分类Dev

如何在python中计算有关负值的指数?

Related 相关文章

热门标签

归档