繁体   English   中英

如何用Python完成此“剪刀石头布”游戏?

[英]How do I finish this Rock, Paper, Scissors game in Python?

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()后如何使用该信息结束游戏?

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

代替

while True:

试试这个(这不会对输入进行错误检查):

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

这是伪代码。

userweapon = str(input()) ,询问用户他们想玩多少个游戏。 类似usergames = int(input()) 创建一个游戏变量,然后放置while True: :。 在每个循环的末尾,增加gamesplayed = gamesplayed + 1它将弹出并检查while处于True状态,​​直到玩了所需的游戏次数。

一起去吧。

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关键字将整个循环移到一个函数中 ,然后在退出循环播放器询问播放器是否要再次播放后,再次调用该函数。 哦,开始时会有一些凌乱的代码是可以的。 ;)我们所有人都必须从某个地方开始,而您的情况还算不错。 保持良好的工作状态,您将立即获得最好的成绩!

使用for或while循环:

while a!=rounds:
    *your code*

要么

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM