import random
rnd=0
guessesTaken = 0
print('Hello! What is your name?')
myName = input()
for i in range (10):
print('---round' +str(rnd+1) +'---')
number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')
while guessesTaken <= 5:
***~the error ^^^~***
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:
break
if guess == number:
guessesTaken = str(guessesTaken)
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')
if guess != number:
number = str(number)
print('Nope. The number I was thinking of was ' + number)
rnd=rnd+1
I am trying to put a round system into this guessing game but at round two after it says "Well, aidan, I am thinking of a number between 1 and 20." there is an error saying
TypeError: '<=' not supported between instances of 'str' and 'int'
from line 15.
When you are saying guessesTaken = str(guessesTaken), the original guessesTaken variable, which was an integer, becomes a string. Then, when you're verifying a second time your while loop condition guessTaken <= 5, you are comparing a string with an integer... which is not something that is supported, as your error stated '<=' not supported between instances of 'str' and 'int'.
The solution would be, as one stated in the comment, to have a better understanding of string formatting, and printing usage. You don't need to transform entirely your variable (meaning transforming your integer into a string).
You could just do instead of...
guessesTaken = str(guessesTaken)
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')
This :
print('Good job, {} ! You guessed my number in {} guesses !'.format(myName, guessesTaken))
Doing this, you are simply printing your variable as is, instead of casting it into another type. If you would still do it your way (which is bad, by the way), you could do...
guessesTaken = str(guessesTaken)
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')
guessesTaken = int(guessesTaken)
Related
I have tried everything i can still doesn't work. Please any help would be appreciated.
Please i have been able to allow it generate one hint, but i need it to generate 3 different hints the 3 different times the user presses 0 for the hint.
I have it give the hint even OR odd. i need it to give extra 2 hints.
Part 1:
Generates a random number between 1 and 100.
Allows the user 10 tries to guess what the number is.
Validates the user input (if user-input >100 or user-input<0) then this is invalid input and should not cost the user to lose any tries.
Gives feedback for each time the user makes a guess. The feedback tells the user whether the number entered is bigger, smaller, or equal to the number generated (and exits the program).
Tells the user if they lost after he/she consumes all the 10 tries. Gives the user 10 tries to guess the number. If the user exhausts the 10 ties. The user loses.
Part 2:
After 2 unsuccessful tries, the program should start offering hints for the users (by having the user input the number 0).
Each hint should be generated within a function of its own.
Each hint will cost the user two tries (the program should indicate this to the user)
The user is allowed a max of 3 hints only.
The program should randomly pick which hint it is going to use and display to the user.
(example of a hint is : 1- The number is bigger than or equal the square of some X (X is an integer and is the largest integer square that is less than the user input))
Here is my program so far:
import random
guessesTaken = 0
print('WELCOME! What is your name?')
myName = input()
number = random.randint(1, 100)
print('Hello, ' + myName + ', I generated a number between 1 and 100.')
unsuccessful_tries = 0
hint_taken = 0
while guessesTaken < 10:
if unsuccessful_tries > 1 and hint_taken<3:
print('Press 0 to get hint')
need_hint = int(input())
if need_hint == 0:
hint_taken += 1
guessesTaken += 1
if number%2==0:
print('The Generated number is an EVEN number')
else:
print('The Generated number is an ODD number')
print('Take a guess.\t%d Attempts Left'%(10 - guessesTaken))
#10-guessTaken gives the number of tries left
guess = input()
guess = int(guess)
#validating the user's input
if guess >100 or guess<0:
continue
guessesTaken = guessesTaken + 1
if guess < number:
print('Your guess is too low.')
if guess > number:
print('Your guess is too high.')
if guess == number:
break
unsuccessful_tries+=1
if guess == number:
guessesTaken = str(guessesTaken)
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')
if guess != number:
number = str(number)
print('Nope. The number I was thinking of was ' + number)
Let me nudge you towards solving part two:
You're already keeping track of the user guesses with the guessesTaken variable
A random 'Hint' can be simply randomly choosing through a list of pre-made hints ( if you had 5 pre-made hints, you could just choose a random number between 1-5, and select that one)
'Costing' the user two tries is as evaluating whether they have enough guesses to afford it ( i.e: if they are at guess number 9, they can't afford to give up 2 guesses), and then add two to the guessNumber if they accept a hint.
You can evaluate the amount of hints taken with a counting variable, though given your parameters (they have to have two unsuccessful guesses to receive a hint, with a maximum of 3 hints total), they would not be able to have 3 hints.
This is the most exact answer i can give you, since you haven't provided any code for part 2 that isn't working or that you need help with.
loose example for point #2: choose a random function from a list:
my_list = [func_test_1, func_test_2, func_test_3]
random.choice(my_list)()
You should create variables which keep tracks of the number of unsuccessful tries and hint taken. If the number of unsuccessful tries is greater than 2 and hint taken is less than 3 then you should ask the user if he wants hint.
import random
guessesTaken = 0
print('Hello! What is your name?')
myName = input()
number = random.randint(1, 100)
print('Hello, ' + myName + ', I am thinking of a number between 1 and 100.')
unsuccessful_tries = 0
hint_taken = 0
while guessesTaken < 10:
if unsuccessful_tries > 1 and hint_taken<3:
print('Press 0 to get hint')
need_hint = int(input())
if need_hint == 0:
hint_taken += 1
guessesTaken += 1
print('Here is hint')
#Do this by yourself chose a hint and display
print('Take a guess.\t%d Attempts Left'%(10 - guessesTaken))
#10-guessTaken gives the number of tries left
guess = input()
guess = int(guess)
#validating the user's input
if guess >100 or guess<0:
continue
guessesTaken = guessesTaken + 1
if guess < number:
print('Your guess is too low.')
if guess > number:
print('Your guess is too high.')
if guess == number:
break
unsuccessful_tries+=1
if guess == number:
guessesTaken = str(guessesTaken)
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')
if guess != number:
number = str(number)
print('Nope. The number I was thinking of was ' + number)
This question already has answers here:
Can't send input to running program in Sublime Text
(5 answers)
Closed 6 years ago.
I have made a guessing game program but every time I run it on VS code or Sublime it only displays the first output and hangs. If I use Python IDLE it runs my whole program. Here's the code below:
# This is a guess the number game.
import random
guessesTaken = 0
print('Hello! What is your name?')
myName = input()
number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')
while guessesTaken < 6:
print('Take a guess.')
guess = input()
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number:
print('Your guess is too low.') #There are eight spaces in front of print
if guess > number:
print('Your guess is too high.')
if guess == number:
break
if guess == number:
guessTaken = str(guessesTaken)
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')
if guess != number:
number = str(number)
print('Nope. The number I was thinking of was ' + number)
I have the solution to your problem.
If you have downloaded sublimeREPL.
Go to tools > SublimeREPL > Python > Python - RUN current file
if not then copy this package control:
https://packagecontrol.io/installation
Go to View > show console > and copy the link above > click Enter and when it is done close sublime and open it again.
Then go to > Preferences > Package control > and type > sublimerepl
Once you have donwloaded you can follow the steps above.
hope it works.
I have a little problem with my code, i'm trying to make a guess game, actually it is from a book, but i can't figure out what is wrong with it...
# A guess game program made in python
import random
guessesTaken = 0
print('Hello! What is your name, may i ask?')
myName = input()
number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20')
while guessesTaken < 6:
print('Take a guess..')
guess = input()
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number:
print('Your number guess is too low, guess again')
if guess > number:
print('Your number is too high! guess lower or something!')
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken)
print('Good job, ' + myName + '! You guessed the right number in' + guessesTaken + 'guesses!')
if guess != number:
number = str(number)
print('Nah, The number i was thinking of was ' + number)
This is the error it's giving me..
Hello! What is your name, may i ask?
ygh
Well, ygh, I am thinking of a number between 1 and 20
Take a guess..
4
Your number guess is too low, guess again
Nah, The number i was thinking of was 7
Take a guess..
2
Traceback (most recent call last):
File "C:/Users/Owner/Desktop/guess.py", line 19, in <module>
if guess < number:
TypeError: unorderable types: int() < str()
Process finished with exit code 1
I'm using Pycharm as my IDLE and i'm also on windows..
Few changes in your code tho, instead of calling str you can use format()
# A guess game program made in python
import random
guessesTaken = 0
print('Hello! What is your name, may i ask?')
myName = input()
number = random.randint(1, 20)
print('Well, {}, I am thinking of a number between 1 and 20'.format(myName))
while guessesTaken < 6:
print('Take a guess..')
guess = input()
guess = int(guess)
guessesTaken += 1 # Instead of calling the variable itself then adding 1
if guess < number:
print('Your number guess is too low, guess again')
if guess > number:
print('Your number is too high! guess lower or something!')
if guess == number:
print('Good job, {}! You guessed the right number in {} guesses!'.format(myName,guessesTaken))
break # the beak goes here
if guess != number:
print('Nah, The number i was thinking of was {}'.format(number))
Also, your break was placed incorrectly because it will be executed before sending the print that you want, thus ending your code prematurely.
I'm writing the radius guessing game and stumbled into the looping problem.
It might seem like a silly question but is the break placing right? or should it be something else?
import random
import math
number = random.randint(1, 20)
area = math.pi * number**2
guessesTaken = 0
print '%.0f is the area' % (area)
print('Take a guess.')
guess = input()
guess = int(guess)
guessesTaken = guessesTaken + 1
while guessesTaken < 6:
if guess < number:
print('Your guess is too low.')
if(guess > number):
print('Your guess is too high.')
if guess == number:
guessesTaken = str(guessesTaken)
print('Good job! You guessed my number in ' + guessesTaken + ' guesses!')
break
if guess != number:
number = str(number)
print('Nope. The number I was thinking of was ' + number)
You should place the last two ifs outside the while loop. Moreover, inside the while you should re-ask for input if the user guessed incorrectly, otherwise guesses never changes and you'd just loop forever.
If you want to put a maximum number of iterations you should increment guessesTaken inside the loop.
while guessesTaken < 6:
if guess < number:
print('Your guess is too low.')
elif guess > number:
print('Your guess is too high.')
else:
break
guess = int(input('Try again: '))
guessesTaken += 1
if guess == number:
print('Good job! You guessed my number in {} guesses!'.format(guessesTaken))
else:
print('Nope. The number I was thinking of was {}'.format(number))
Anyway, if you know you'll have at most 6 iterations you should just use a for loop instead:
for _ in range(6): # loop 6 times
if guess < number:
print('Your guess is too low.')
elif guess > number:
print('Your guess is too high.')
else:
break
guess = int(input('Try again: '))
i was trying to create a number guessing game but I get the error for "guessestaken"I copied the code from http://inventwithpython.com/IYOCGwP_book1.pdf page 57.Sorry I am a bit new to python.
import random
guessestaken=0
print ("hello what ur name?")
myname=input()
number=random.randint(1,20)
print ("well " + myname + " i am thinking of a number guess it")
while guessestaken < 6 :
guessestaken=guessestaken+1
guess =input('take a guess')
guess = int(guess)
if guess <number:
print('too low')
if guess >number:
print ('too high')
if guess ==number:
break
if guess ==number:
guessestaken=str(guessestaken)
print ('good job ' + myname + ' you are right!')
print ('you guessed it in ' + guessestaken + ' guesses')
if guess !=number:
guessestaken = str(guessestaken)
print ("I am sorry but you couldn't get it right")
print ("you couldn't guess it in " + guessestaken + " guesses")
The error message is (trying to) inform you that you're trying to compare a str with an int. In particular, there should be a traceback informing you of where the error occurs:
Traceback (most recent call last):
File "./tmp.py", line 8, in <module>
while guessestaken < 6 :
You can see that you explicitly convert guessestaken to a string:
guessestaken = str(guessestaken)
Which is clearly not necessary. When you want to print the number of guesses taken, either do it inline using + (which is not recommended or "pythonic") or use format:
print('you guessed it in ' + str(guessestaken) + ' guesses')
print('You guessed it in {} guesses'.format(guessestaken))
You are converting guesstaken to string to display it
guessestaken=str(guessestaken)
and then while loop checks whether
while guessestaken < 6 :
which causes type error (python cannot compare string and int).
You should simply use other name for the string or do it inline using python constructs like
print('You guessed it in {} guesses'.format(guessestaken))