我如何改善有关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 条评论
登录 后参与评论

相关文章

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

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

我不明白有关 while 循环的问题

Python 代码:有关循环/条件的执行跟踪的信息

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

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

我如何告诉Visual Studio代码有关相对路径

我需要有关如何优化代码的建议。执行时间太长

我该如何解释这段代码?(与修剪有关)

我不了解有关递归(python)的那部分代码

如何在while循环中使用很多OR条件来改善我的LCM代码?

有关动态编程的作业。使我的代码更高效?

我碰到了有关保管箱的代码

我需要有关此代码的帮助

如何在Keras中了解有关简单中性网络Python代码的密集层参数

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

如何理解以下有关golang slice的代码?

有关如何使onResume()运行onCreate()代码的问题

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

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

有关转换为数组的Python代码

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

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

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

我如何看待有关PostgreSQL约束的评论?

我需要有关如何正确解析的帮助

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

我如何收集有关网站/网页内容的数据

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