无论我将变量更改为 str 还是 int,终端都会给我一个语法错误
No matter if I change my variable to str or int, terminal will give me a syntax error
我对 运行 代码在终端中不断收到的响应感到有些困惑。我正在制作一个具有简单输入界面的简单游戏,我想为玩家展示他们在该关卡中用完了多少次尝试。这是我的全部代码:
import random
import time
# I will fist make variables for the time breaks. S is for short, M is for medium and L is for long
S = 0.2
M = 0.7
L = 1.1
class Level_1_activated():
def get_name(self):
# This function simply asks the name of the player
name = input("Before we start, what is your name? ")
time.sleep(S)
print("You said your name was: " + name)
def try_again(self):
# This asks the player if they want to try again, and shows the progress of the level
answer = (input("Do you want to try again? "))
time.sleep(M)
if answer == "yes":
print("Alright!, well I am going to guess that you want to play again")
time.sleep(M)
return True
else:
print("Thank you for playing the game, I hope you have better luck next time")
# This is the return statement that stops the while loop
return False
def find_rand_num(self, random):
# This is the core of the level, where the player just chooses numbers between 1 and 10
time.sleep(S)
print("The computer is choosing a random number between 1 and 10... beep beep boop")
time.sleep(L)
# The list of numbers for the level that the player is on at the moment
num_list = [1,10]
number = random.choice(num_list)
ques = (input("guess your number, since this is the first level you need to choose a number between 1 and 10 "))
print(ques)
if ques == str(number):
time.sleep(S)
print("Congratulations! You got the number correct!")
time.sleep(L)
print("Good job! You will be progressing to the 2nd level now!")
time.sleep(L*2)
# Yet another return statement for the while loop
return "Found"
elif input != number:
time.sleep(M)
print("Oops, you got the number wrong")
def run_game():
tries = 1
while tries < 6:
if tries < 2:
Level_1_activated().get_name()
print("You have used up " + tries + " of your tries already")
res = Level_1_activated().find_rand_num(random)
if res == "Found":
break
checker = Level_1_activated().try_again()
if checker is False:
break
tries += 1
print("Remember you have used up " + tries + " of your tries")
if __name__ == "__main__":
run_game()
我会将代码缩小到我认为更相关的地方:
tries = 1
while tries < 6:
if tries < 2:
Level_1_activated().get_name()
print("You have used up " + tries + " of your tries already")
res = Level_1_activated().find_rand_num(random)
if res == "Found":
break
checker = Level_1_activated().try_again()
if checker is False:
break
tries += 1
print("Remember you have used up " + tries + " of your tries")
if __name__ == "__main__":
run_game()
我在这一行中遇到错误:
print("Remember you have used up " + tries + " of your tries")
说的是:TypeError:只能将 str(不是“int”)连接到 str。我试过改
tries
至:
str(tries) or even int(tries)
我不确定如何解决这个问题。感谢您抽出宝贵时间,希望您能提供解决我错误的答案。
print("Remember you have used up {} of your tries".format(tries))
print("Remember you have used up {t} of your tries".format(t=tries))
您需要格式化字符串,将值传递给占位符。
当我在打印行中使用 str(tries) 时,它对我来说工作正常。
tries=10
print("Remember you have used up " + str(tries) + " of your tries")
输出
Remember you have used up 10 of your tries
能否请您再检查一次代码,我认为它应该可以正常工作。
我对 运行 代码在终端中不断收到的响应感到有些困惑。我正在制作一个具有简单输入界面的简单游戏,我想为玩家展示他们在该关卡中用完了多少次尝试。这是我的全部代码:
import random
import time
# I will fist make variables for the time breaks. S is for short, M is for medium and L is for long
S = 0.2
M = 0.7
L = 1.1
class Level_1_activated():
def get_name(self):
# This function simply asks the name of the player
name = input("Before we start, what is your name? ")
time.sleep(S)
print("You said your name was: " + name)
def try_again(self):
# This asks the player if they want to try again, and shows the progress of the level
answer = (input("Do you want to try again? "))
time.sleep(M)
if answer == "yes":
print("Alright!, well I am going to guess that you want to play again")
time.sleep(M)
return True
else:
print("Thank you for playing the game, I hope you have better luck next time")
# This is the return statement that stops the while loop
return False
def find_rand_num(self, random):
# This is the core of the level, where the player just chooses numbers between 1 and 10
time.sleep(S)
print("The computer is choosing a random number between 1 and 10... beep beep boop")
time.sleep(L)
# The list of numbers for the level that the player is on at the moment
num_list = [1,10]
number = random.choice(num_list)
ques = (input("guess your number, since this is the first level you need to choose a number between 1 and 10 "))
print(ques)
if ques == str(number):
time.sleep(S)
print("Congratulations! You got the number correct!")
time.sleep(L)
print("Good job! You will be progressing to the 2nd level now!")
time.sleep(L*2)
# Yet another return statement for the while loop
return "Found"
elif input != number:
time.sleep(M)
print("Oops, you got the number wrong")
def run_game():
tries = 1
while tries < 6:
if tries < 2:
Level_1_activated().get_name()
print("You have used up " + tries + " of your tries already")
res = Level_1_activated().find_rand_num(random)
if res == "Found":
break
checker = Level_1_activated().try_again()
if checker is False:
break
tries += 1
print("Remember you have used up " + tries + " of your tries")
if __name__ == "__main__":
run_game()
我会将代码缩小到我认为更相关的地方:
tries = 1
while tries < 6:
if tries < 2:
Level_1_activated().get_name()
print("You have used up " + tries + " of your tries already")
res = Level_1_activated().find_rand_num(random)
if res == "Found":
break
checker = Level_1_activated().try_again()
if checker is False:
break
tries += 1
print("Remember you have used up " + tries + " of your tries")
if __name__ == "__main__":
run_game()
我在这一行中遇到错误:
print("Remember you have used up " + tries + " of your tries")
说的是:TypeError:只能将 str(不是“int”)连接到 str。我试过改
tries
至:
str(tries) or even int(tries)
我不确定如何解决这个问题。感谢您抽出宝贵时间,希望您能提供解决我错误的答案。
print("Remember you have used up {} of your tries".format(tries))
print("Remember you have used up {t} of your tries".format(t=tries))
您需要格式化字符串,将值传递给占位符。
当我在打印行中使用 str(tries) 时,它对我来说工作正常。
tries=10
print("Remember you have used up " + str(tries) + " of your tries")
输出
Remember you have used up 10 of your tries
能否请您再检查一次代码,我认为它应该可以正常工作。