Reputation: 3
I am making improvements on Guess The Number game. Players can type in their own range after the first round. How do I show the input number in the range below?
print("Let's play again !" "I am thinking of a number between 1 and ")
I tried using + but it only seems to work for word variables only.
Here is the code for the relevant section:
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!")
Thank you!
Upvotes: 0
Views: 1237
Reputation: 111
The string formatting answer above is your best practice solution. However, if you are unfamiliar with string formatting and do not yet have time to learn it, you can use the + as you originally wanted by converting the number to a string:
print("I am thinking of a number between 1 and " + str(number))
Punctuation tends to render these constructs unsightly, however:
print("I am thinking of a number between 1 and " + str(number) + ".")
It becomes more and more unsightly when using this method to produce dynamic output, so I suggest making time for the string formatting answer offered by cricket_007
.
Upvotes: 0
Reputation: 191831
Just do string formatting?
"I am thinking of a number between 1 and {}".format(number)
Upvotes: 1