2016-02-14 38 views
0

我正在对猜数字游戏进行改进。玩家可以在第一轮后输入自己的范围。如何在下面的范围中显示输入号码?如何在句子中添加变量输入?

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!") 

谢谢!

回答

1

只是做字符串格式?

"I am thinking of a number between 1 and {}".format(number) 
0

上面的字符串格式化答案是您的最佳实践解决方案。但是,如果你不熟悉的字符串格式化和还没有时间去学习它,你可以使用+,你本来想通过数字转换为字符串:

print("I am thinking of a number between 1 and " + str(number)) 

标点符号往往使这些结构难看然而:

print("I am thinking of a number between 1 and " + str(number) + ".") 

使用这种方法产生的动态输出时,它变得越来越难看,所以我建议把时间由cricket_007提供的字符串格式化的回答。