2014-01-10 41 views
0
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.") 

我該如何要求用戶輸入他們要支付多少分,以及如何在輸入()達到時使用此信息結束遊戲?如何在Python中完成這個Rock,Paper,Scissors遊戲?

我知道這段代碼看起來很糟糕,但我是初學者,這是我能做的最好的。感謝您的閱讀,我會爲最佳答案投票。

回答

0

代替

while True: 

嘗試此(這具有輸入的沒有錯誤檢查):

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

因我的範圍是什麼(VAR):是什麼意思?我看到了很多,但我不明白! – pythonswag

+0

範圍(var)返回從0到var的項目列表。因爲我在範圍內迭代生成的列表,然後放棄列表。 – GoingTharn

+0

只需輸入'range(5)'即可。它打印出[[0,1,2,3,4]',實際上是爲你製作一份清單(比這更復雜一點,但它有這種效果)。 'for'循環將每個元素依次分配給我,直到完成爲止。你可以忽略我,或者你可以打印出來讓用戶知道他們正在玩哪一回合。 – mhlester

0

這是僞代碼。

userweapon = str(input())之前,詢問用戶他們想玩多少遊戲。像usergames = int(input())。創建一個遊戲中的變量,並把while True:。在每個循環結束時,增量gamesplayed = gamesplayed + 1它會彈出並檢查True狀態,​​直到它已經玩完所需數量的遊戲。

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,然後就再次調用該函數詢問後球員,如果他們想一旦你跳出循環再次發揮。 呵呵,可以稍微亂一點的代碼開始。 ;)我們都必須從某個地方開始,而你的情況並不那麼糟糕。保持良好的工作,你會立即在最好的狀態!

0

使用的或while循環:

while a!=rounds: 
    *your code* 

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