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)
Related
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed last month.
For right now, I have it so it will print the random number it chose. However, whether I get it wrong or right it always says I am wrong.
here is my code:
import random
amount_right = 0
number = random.randint(1, 2)
guess = input
print(number)
print(
"welcome to this number guessing game!! I am going to think of a number from 1-10 and you have to guess it! Good luck!")
input("enter your guess here! ")
if guess != number:
print("Not quite!")
amount_right -= 1
print("you have a score of ", amount_right)
else:
print("good Job!!")
amount_right += 1
print("you have a score of ",amount_right,"!")
what did I do wrong? I am using Pycharm if that helps with anything.
I tried checking my indentation, I tried switching which lines were the if and else statements (lines 13 - 21) and, I tried changing lines 18 - 21 to elif: statements
guess = int(input())
You need to convert your guess to int and there should be () in input
Also there are 2 input() in your code. One is unnecessary. This can be the code.
import random
amount_right = 0
number = random.randint(1, 2)
print(number)
print(
"welcome to this number guessing game!! I am going to think of a number from 1-10 and you have to guess it! Good luck!")
guess = int(input("enter your guess here! "))
if guess != number:
print("Not quite!")
amount_right -= 1
print("you have a score of ", amount_right)
else:
print("good Job!!")
amount_right += 1
print("you have a score of ",amount_right,"!")
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 am working on a guessing game in python, i think i have everything only, i want to make the program to guess between numbers it already guessed for example, if the users number is 5, and it picks 3 the user input '+' and it knows the number is higher, and if the program guess 6 the user input '-' and it knows the number is lower than 6, but sometimes it guesses a 2, its obvious that if the number is higher than 3 it can't possibly be 2 right, so how do i write that? I am a beginner at this and i would appreciate if you could make it simple, below is my code.
print("Hello,")
print("welcome to the guessing game")
print('I shall guess a number between 1 and 99, and then ask you if am right')
print('I have a maximum of 20 chances\n')
import random
guess = random.randint(1,99)
print("Your number is %f, Am i right?" % guess)
print('If I am, enter =, If the number is higher enter (+), if the number is lower enter (-)')
ans = input('Which is it: ')
print("You chose %s" % ans)
minguess = 1
maxguess = 99
count = 0
while (count < 20):
count = count + 1
if ans == '+':
##I am using these prints to keep track of the numbers and if everything is working correctly
maxguess1 = guess + 1
print('THe maxguess is', maxguess1)
newguess = random.randint(maxguess1, maxguess)
print('The newguess is', newguess)
newguess = int(newguess)
print("Is it %d?" % newguess)
print('If I am, enter =, If the number is higher enter (+), if the number is lower enter (-)')
ans = input('Which is it: ')
elif ans == "-":
maxguess2 = guess - 1
print('The minus maxguess is', maxguess2)
newguess = random.randint(minguess, maxguess2)
print('The minus newguess is', newguess)
newguess1 = int(newguess)
print("Is it %d?" % newguess1)
print('If I am, enter =, If the number is higher enter (+), if the number is lower enter (-)')
ans = input('Which is it: ')
if ans == "=":
print('YAAAAAAS MAN')
i wanted it to change the numbers whenever it guessed a new number
guess = newguess
NOTE: This example is in Python 2.7, NOT Python 3, but the concepts are the same.
Break down the problem into its individual elements:
import random
# Possible Range is [1-99], 1 inclusive to 99 inclusive
min_possible = 1
max_possible = 99
# Number of Guesses
max_guesses = 20
# Process
for i in xrange(max_guesses): # Loops through the process 'max_guesses' times
# Program Takes a Guess
guess = random.randint(min_possible, max_possible)
print 'My guess is ' + str(guess)
# Ask for User Feedback
user_feedback = ''
while not user_feedback in ['+', '-', '=']:
user_feedback = raw_input('Is the number higher (+), lower (-), or equal (=) to my guess?')
# Use the User Feedback
if user_feedback == '+':
min_possible = guess + 1 # B/c low end is inclusive
elif user_feedback == '-':
max_possible = guess - 1 # B/c high end is inclusive
else:
print 'I knew the answer was ' + str(guess)
break
I created a guess the number program and used a while loop to allow the user to continue guessing until he/she could get it right, as seen here:
import random
number = random.randrange(1, 6)
print "Guess the number, between 1 and 6"
guess = "yes"
while guess != number:
guess = int(raw_input('>'))
if guess == number:
break
print "Good job! You got it right!"
print number
elif guess > number:
print "Too High"
print number
number = random.randrange(1, 6)
elif guess < number:
print "Too Low"
print number
number = random.randrange(1, 6)
The problem is, when I am trying to guess the number, it will randomly end, whether me guessing the first time, 4 times, or 30 times. Also, I originally had,
guess = int(raw_input('>'))
in place of,
guess = "yes"
and replaced it to get rid of the extra and useless raw_input i'd initially need to add into terminal. Why am i able to make it equal "yes" and why doesn't it matter what I put there?
Example of Bug:
Guess the number, between 1 and 6
>3
Too High
2
>4
Too Low
5
>6
Too High
5
>3
Too High
1
>2
Too High
1
>5
Good job! You got it right!
5
------------------
(program exited with code: 0)
Press return to continue
It worked that time, and now:
Guess the number, between 1 and 6
>3
Too Low
4
------------------
(program exited with code: 0)
Press return to continue
The issue you have is that your while loop is testing if number matched guess after picking a new number value but before getting a new guess. This means that you'll say the player guessed wrong, but they they become right afterwards and the loop will end.
Try this instead:
import random
print "Guess the number, between 1 and 6"
guess = 'y' # the values set here don't actually matter, they just need to be different
number = 'x'
while guess != number:
number = random.randint(1, 6)
guess = int(raw_input('>'))
if guess == number:
print "Good job! You got it right!"
elif guess > number:
print "Too High"
print number
elif guess < number:
print "Too Low"
print number
I've also changed your use of random.randrange to random.randint, which will make it actually return 6s some of the time (randrange excludes the upper bound).
guess = "yes" works because guess != number will always be True on the first check in the while loop, at which point the user is asked for input. Also, in the if block, put the break statement after all the print's.
import random
number = random.randrange(1, 6)
print "Guess the number, between 1 and 6"
guess = "yes"
while guess != number:
guess = int(raw_input('>'))
if guess == number:
break
print "Good job! You got it right!"
print number
break
elif guess > number:
print "Too High"
print number
number = random.randrange(1, 6)
break
elif guess < number:
print "Too Low"
print number
number = random.randrange(1, 6)
break
I have a quick question.
I want to make my number guessing game tell the user if they are 2 numbers away from guessing the random number.
I do not want the program to say how many numbers away the user is.
The way I'm thinking is this. I just can't put this into proper code.
Random_number = 5
guess = 3
print('you are close to guessing the number!')
guess = 7
print('you are close to guessing the number!')
guess = 2 #more than 2 away from the number
print('you are NOT close to guessing the number')
I am going to start by saying my python is rusty and someone please fix it if im off alittle.
All you need to do if use an if statement.
random = 5
guess = 3
if( guess == random ):
print('your right!')
elif ( abs (guess - random) <= 2 ):
print('you are close to guessing the number!')
else:
print('you are not close enough!')
Edited the elseif logic according to #9000's suggestion.
Try this:
for number in range (2):
if guess == Random_number:
print('you guessed the number!')
if guess - number == Random_number or guess + number == Random_number:
print('you are close to guessing the number!)
Here is the explanation. The first line is saying "for the numbers in the range of 0 to 2 set number equal to that number." So the next if part will run 3 times: for number equaling 0, 1, and 2. So, if your guess is within 2 of the random number, it will print you are close to the random number. If you have the correct guess, it will obviously print you guessed it correctly.