如何改进关于while循环的python代码

时间:2016-11-15 02:15:42

标签: python-2.7

有人可以查看我的代码并告诉我一些方法可以使我的代码更高效或更短。我的程序基本上生成1到6之间的2个数字并取其总和。如果总和等于3,7,11,则该程序会说"你赢了"。如果总和是一个奇数,它会说"你输了#34;。如果总和是偶数,则表示" draw"。最后,它显示赢得的游戏数量和赢得的游戏百分比。如果他们想要再次发挥更高效率的话,我怎么能让它成为用户询问的部分。(仍然是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)

1 个答案:

答案 0 :(得分:3)

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

您可以使用lower(),然后您不必与上方N进行比较。您可以使用strip(),因为有时人们可以将空间放在答案中而不会看到这一点。

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

请参阅PEP 8 -- Style Guide for Python Code

  • 使用变量的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)总是给出相同的结果。