Python石剪功能

2024-04-18 23:56:46 发布

您现在位置:Python中文网/ 问答频道 /正文

试图建立一个非常基本的石头,布,剪刀代码,但添加功能后,似乎不起作用,谁能告诉我,为什么?在

print "1 stands for paper, 2 stands for rock, 3 stand for scissors"
signs = [1, 2, 3]
gaming = 1
def game():
    from random import choice
    pc = raw_input("pick a sign, use the numbers shown above ")
    i = int(pc)
    cc = choice(signs)
    if i - cc == 0 : # 3 values
        print "it's a draw"
    elif i - cc == 1 : # 2 values
        print "you lose"
    elif  i - cc == 2 : # 1 value
        print "you win"
    elif i - cc == -1 : # 2 values
        print "you win"
    elif i - cc == -2 : # 1 value
        print "you lose"
    gamin = raw_input("if you want to play again, press 1")
    gaming = int(gamin)
while gaming == 1 :
    game

Tags: yougameforinputrawintccvalues
1条回答
网友
1楼 · 发布于 2024-04-18 23:56:46

据我所知,你的问题是你没有打电话给game。添加()以调用函数:

while gaming == 1:
    game()

但是,您还需要重新构造while循环,并使game返回{}。另外,为了提高效率,你应该做一些改变。我重写了你的程序来解决这些问题:

^{pr2}$

注意,我去掉了一些变量。在这种情况下,创建它们对脚本没有任何积极的贡献。删除它们可以清理代码并提高效率。在

相关问题 更多 >