我正在製作一個模擬Roshambo(岩石,紙,剪刀)的遊戲。 一切都很好,除非你贏了一場比賽,我得到一個錯誤。這是我的代碼。無法解決我的錯誤。 TypeError:'int'類型的對象沒有len()
###import the random module
import random
import time
##my function
def RockPaperScissors():
print("This is Roshambo Simulator 2016 copyright © Jared is Cool")
time.sleep(2.5)
print("Your score is on the left, and the computers is on the right.")
time.sleep(1.25)
score = [0,0]
print(score)
cpu = 1, 2, 3
### game loop! you choose your weapon
while True:
while True:
choice = input("Rock(1), Paper(2), or Scissors(3)?")
if choice == "1":
whichOne = 1
break
elif choice == "2":
whichOne = 2
break
elif choice == "3":
whichOne = 3
break
##who wins the roshambo
cpu = random.choice(cpu)
if whichOne == cpu:
print("stale!")
print(score)
elif (whichOne == 3 and cpu == 2) or (whichOne == 2 and cpu == 1) or (whichOne == 1 and cpu == 3):
print("win!")
score[0] = score[0] + 1
print(score)
else:
print("lose")
print(score)
score[1] = score[1] + 1
RockPaperScissors()
這是我不斷收到的錯誤。
Traceback (most recent call last):
File "python", line 41, in <module>
File "python", line 28, in RockPaperScissors
TypeError: object of type 'int' has no len()
cpu = random。選擇(cpu)''在這裏你可以用選項(一個整數)替換你的選擇列表。嘗試'cpu_choice = random.choice(cpu)'並替換下面的'cpu'-名稱。 :) – MSeifert
'cpu = random.choice(cpu)'正在用一個選項替換你的元組'1,2,3''。通過循環第二次,你會得到你報告的錯誤。一個好的調試技術是在發出錯誤的行之前加上'print'語句來檢查你的變量是否包含你期望的內容。 –
感謝MSeifert的幫助,並感謝Paul Hankin的建議。這解決了我的問題。做得好! – jrod