我如何用Python完成这个Rock,Paper,Scissors游戏?

时间:2014-01-10 19:33:50

标签: python python-3.x

import random

userscore = 0
computerscore = 0

print("Welcome to the Rock, Paper, Scissors Game")
while True:
    print("User Score = ", userscore, " Computer Score = ", computerscore)
    print("Rock, Paper or Scissors?")

    userweapon = str(input())
    print("You chose ", userweapon)

    computerweapon = random.randint(1,3)

    if computerweapon == 1:
        computerweapon = "Rock"
    elif computerweapon == 2:
        computerweapon = "Paper"
    else:
        computerweapon = "Scissors"

    print("Computer chose ", computerweapon)

    if userweapon == computerweapon:
        print("You chose the same Weapon, it's a draw.")
    elif userweapon == "Rock" and computerweapon == "Paper":
        print("Computer Point.")
        computerscore +=1
    elif userweapon == "Rock" and computerweapon == "Scissors":
        print("User Point.")
        userscore +=1
    elif userweapon == "Paper" and computerweapon == "Rock":
        print("User Point.")
        userscore +=1
    elif userweapon == "Paper" and computerweapon == "Scissors":
        print("Computer Point.")
        computerscore +=1
    elif userweapon == "Scissors" and computerweapon == "Rock":
        print("Computer point.")
        computerscore +=1
    elif userweapon == "Scissors" and computerweapon == "Paper":
        print("User Point.")

如何让用户键入他们想要支付多少积分以及如何在达到input()时使用此信息结束游戏?

我知道这段代码看起来很糟糕,但我是初学者,这是我能做的最好的。感谢阅读,我会投票给出最佳答案。

4 个答案:

答案 0 :(得分:0)

而不是

while True:

试试这个(这没有错误检查输入):

rounds = int(input('How many rounds do you want to play?'))
for i in range(rounds):

答案 1 :(得分:0)

这是伪代码。

userweapon = str(input())之前,询问用户他们想玩多少游戏。像usergames = int(input())这样的东西。创建一个游戏玩法变量并放置while True:。在每个循环结束时,增加gamesplayed = gamesplayed + 1它会弹出并检查while状态,直到它播放了所需数量的游戏。

答案 2 :(得分:0)

顺便说一句。

userscore = 0
computerscore = 0
winscore = 0 ## This will store the number of points we play to

print("Welcome to the Rock, Paper, Scissors Game")
print("How many points would you like to play to?")
winscore = int(input()) ## Takes input and casts it to an int. This will raise a ValueError if the user inputs anything other than a number.

while True:
    if userscore == winscore:
        print("You win!")
        break ## Break will stop loops(for, while) running, like return does in a function
    elif computerscore == winscore:
        print("You lost :(")
        break

    print("User Score = ", userscore, " Computer Score = ", computerscore)
    print("Rock, Paper or Scissors?")

    ## Weaponising and fighting go here ##
    ## ... ##

如果你愿意,你甚至可以使用def关键字将整个循环移动到function,然后在询问玩家是否想要再次玩游戏后再次调用该函数跳出循环。 哦,开始时有一些稍微凌乱的代码是可以的。 ;)我们都必须从某个地方开始,你的甚至不是那么糟糕。保持良好的工作,你会立即与最好的人在一起!

答案 3 :(得分:0)

使用for或while循环:

while a!=rounds:
    *your code*

for i in range (0, rounds):
    *your code*
    i=i+1