Python code about guessing game - python

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.

Related

I am making a guessing game but I have problems with it. What should I do?

So this is my code:
import random
def guess(x):
random_number = random.randint(1, x)
guess = 0
while guess != random_number:
guess = input(f'Guess a number between 1 and {x}:')
if guess < random_number:
print('Sorry, Guess again. Too low.')
elif guess > random_number:
print('Sorry. Guess again. Too high')
print(f'Yay, Congrats. You have guessed the number {random_number} correctly!')
guess(10)
but I am getting these errors:
line 15, in <module>
guess(10)
and
line 8, in guess
if guess < random_number:
TypeError: '<' not supported between instances of 'str' and 'int'
This should work:
import random
def guess(x):
random_number = random.randint(1, x)
guess = 0
while guess != random_number:
guess = int(input(f'Guess a number between 1 and {x}:'))
if guess < random_number:
print('Sorry, Guess again. Too low.')
elif guess > random_number:
print('Sorry. Guess again. Too high')
print(f'Yay, Congrats. You have guessed the number {random_number} correctly!')
guess(10)
Output:
Guess a number between 1 and 10: 4
Sorry, Guess again. Too low.
Guess a number between 1 and 10: 6
Sorry. Guess again. Too high
Guess a number between 1 and 10: 5
Yay, Congrats. You have guessed the number 5 correctly!
Your code had two problems: the indentation was not correct, and everything read from input() is a string. To fix that I just converted the number read from input() to an int.
While catasaurus' answer is correct in the syntax errors, the function to print both the guess is too high and the guess is too low is redundant.
Try something like this...
import random
def guess(x):
random_number = random.randint(1, x)
guess = 0
while guess != random_number:
guess = int(input(f'Guess a number between 1 and {x}:'))
if guess < random_number:
print('Sorry, Guess again. Too low.')
if guess > random_number:
print('Sorry. Guess again. Too high'
print(f'Yay, Congrats. You have guessed the number {random_number} correctly!')
guess(10)
It returns...
Guess a number between 1 and 10:7
Sorry. Guess again. Too high
Guess a number between 1 and 10:5
Sorry, Guess again. Too low.
Guess a number between 1 and 10:6
Yay, Congrats. You have guessed the number 6 correctly!

trouble with my random number games round system

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)

Python program that: Generates a random number between 1 and 100

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)

I'm trying to write a number guessing game in python but my program isn't working

The program is supposed to randomly generate a number between 1 and 10 (inclusive) and ask the user to guess the number. If they get it wrong, they can guess again until they get it right. If they guess right, the program is supposed to congratulate them.
This is what I have and it doesn't work. I enter a number between 1 and 10 and there is no congratulations. When I enter a negative number, nothing happens.
import random
number = random.randint(1,10)
print "The computer will generate a random number between 1 and 10. Try to guess the number!"
guess = int(raw_input("Guess a number: "))
while guess != number:
if guess >= 1 and guess <= 10:
print "Sorry, you are wrong."
guess = int(raw_input("Guess another number: "))
elif guess <= 0 and guess >= 11:
print "That is not an integer between 1 and 10 (inclusive)."
guess = int(raw_input("Guess another number: "))
elif guess == number:
print "Congratulations! You guessed correctly!"
Just move the congratulations message outside the loop. You can then also only have one guess input in the loop. The following should work:
while guess != number:
if guess >= 1 and guess <= 10:
print "Sorry, you are wrong."
else:
print "That is not an integer between 1 and 10 (inclusive)."
guess = int(raw_input("Guess another number: "))
print "Congratulations! You guessed correctly!"
The problem is that in a if/elif chain, it evaluates them from top to bottom.
Move the last condition up.
if guess == number:
..
elif other conditions.
Also you need to change your while loop to allow it to enter in the first time. eg.
while True:
guess = int(raw_input("Guess a number: "))
if guess == number:
..
then break whenever you have a condition to end the game.
The problem is that you exit the while loop if the condition of guessing correctly is true. The way I suggest to fix this is to move the congratulations to outside the while loop
import random
number = random.randint(1,10)
print "The computer will generate a random number between 1 and 10. Try to guess the number!"
guess = int(raw_input("Guess a number: "))
while guess != number:
if guess >= 1 and guess <= 10:
print "Sorry, you are wrong."
guess = int(raw_input("Guess another number: "))
elif guess <= 0 and guess >= 11:
print "That is not an integer between 1 and 10 (inclusive)."
guess = int(raw_input("Guess another number: "))
if guess == number:
print "Congratulations! You guessed correctly!"

Radius guessing game loop going wrong

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: '))

Categories