我正在改进Guess The Number游戏。玩家可以在第一轮后输入自己的射程。如何在下面的范围内显示输入数字?
print("Let's play again !" "I am thinking of a number between 1 and ")
我尝试使用+但它似乎只适用于单词变量。
以下是相关部分的代码:
print ("Do you want to play again?" " Yes/No")
Play = input()
if Play.lower() == "yes":
print ("What do you want your range to be?" " 1-?")
import random
correctGuess = False
guessesTaken = 0
number = random.randint(1, int(input( "Enter a number "))+1)
print("Let's play again !" "I am thinking of a number between 1 and ")
while correctGuess == False:
print("Take a guess.")
guess = input()
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number:
print("Your guess is too low.")
if guess > number:
print("Your guess is too high.")
if guess == number:
correctGuess = True
guessesTaken = str(guessesTaken)
print("Congrats, " + myName + "! You guessed my number in " + guessesTaken + " guesses!")
else:
print ("Thanks for playing!" " Hope to see you soon!")
谢谢!
答案 0 :(得分:1)
只是做字符串格式化?
"I am thinking of a number between 1 and {}".format(number)
答案 1 :(得分:0)
上面的字符串格式化答案是您的最佳实践解决方案。但是,如果您不熟悉字符串格式并且还没有时间学习它,可以通过将数字转换为字符串来使用原来想要的+:
B1 ------C1
/ \
A --- B --- C ---- D --
标点符号往往会使这些结构难看,但是:
print("I am thinking of a number between 1 and " + str(number))
使用此方法生成动态输出时变得越来越难看,所以我建议为print("I am thinking of a number between 1 and " + str(number) + ".")
提供的字符串格式化答案腾出时间。