Python guessing game with clues - python

A Python(3.7) beginner here. This guessing game gives range clues:
Cold
Warm
Hot
Depending on how close to answer player is.
Problem: how to add extra 3 incremental clues:
Colder
Warmer
Hotter
Colder is used if the next guess is further from answer.
Warmer is used if the next guess is closer to answer.
Hotter is used instead of Warmer if its in the Hot range.
The first guess produces the range clues Cold, Warm or Hot.
The subsequent guesses will produce incremetal clues Colder or Warmer/Hotter if while they land in same range as previous guess.
If they fall out of the range, the range clues Cold, Warm or Hot will be produced first and then Colder or Warmer/Hotter while in that range. in other words Cold, Warm or Hot range clues have higher priority than incremental Colder or Warmer/Hotter.
print("The secret number is somewhere between 0 and 100. You have 5 guesses.")
user_input = int(input('Make a guess '))
count = 0
while user_input is not 41 and count < 4:
count = count + 1
how_close_to_answer = 41 - user_input
if 5 < how_close_to_answer.__abs__() < 20:
user_input = int(input(f'Warm. Remaining guesses {5 - count} '))
elif how_close_to_answer.__abs__() >= 20:
user_input = int(input(f'Cold. Remaining guesses {5 - count} '))
else:
user_input = int(input(f'Hot. Remaining guesses {5 - count} '))
if user_input is not 41:
print('You Lose!')
else:
print('You Win!')
print(f"It took you {count + 1} guesses to get this correct.")
For example (in case of infinite guesses n):
player guesses = 10 , desired outcome 'Cold. Remaining guesses (n-1) '
next guess = 15 , desired outcome 'Warmer. Remaining guesses (n-2) '
next guess = 12 , desired outcome 'Colder. Remaining guesses (n-3) '
next guess = 36 , desired outcome 'Hot. Remaining guesses (n-4) '
next guess = 37 , desired outcome 'Hotter. Remaining guesses (n-5) '
next guess = 30 , desired outcome 'Warm. Remaining guesses (n-6) '
In 4. example - number 36 is Warmer than previous 12, but it also falls in the Hot range so the Hot clue is given instead.
in 6. example - number 30 is Colder than previous 37, but it also falls in the Warm range so the Warm clue is given instead.

I took num as a random generated number instead of 41.
import random
print("The secret number is somewhere between 0 and 100. You have 5 guesses.")
user_input = int(input('Make a guess '))
count = 0
num = random.randint(1,101)
while user_input is not num and count < 4:
#uncomment the line below to see random generated number
#print('Generated Random Number= '+str(num))
count = count + 1
how_close_to_answer = num - user_input
if abs(how_close_to_answer)>5 and abs(how_close_to_answer) <20 :
user_input = int(input(f'Warm. Remaining guesses {5 - count} '))
elif abs(how_close_to_answer) >= 20 :
user_input = int(input(f'Cold. Remaining guesses {5 - count} '))
else:
user_input = int(input(f'Hot. Remaining guesses {5 - count} '))
if user_input is not num:
print('You Lose!')
else:
print('You Win!')
print(f"It took you {count + 1} guesses to get this correct.")
As far as i understood , the above program generates a random number and you need to guess that number ,
if your guessed number is less then or equivalent to 5 digits closer to that random number it will tell you hot
if its greater than 5 and less than 20 then it will tell you warm
on greater than 20 it will give you cold
Hope this will help you !!

This is the closest I could get to what you asked for. The comments on your original question are worth a read in my opinion but I think this does exactly what you asked for in the question.
print("The secret number is somewhere between 0 and 100. You have 5 guesses.")
user_input = int(input('Make a guess '))
count = 0
last_distance = -1
while user_input is not 41 and count < 4:
count = count + 1
how_close_to_answer = (41 - user_input)
how_close_to_answer = how_close_to_answer.__abs__()
if how_close_to_answer <= 5 and last_distance > 5:
user_input = int(input(f'Hot. Remaining guesses {5 - count} '))
elif last_distance == -1:
if 5 < how_close_to_answer < 20:
user_input = int(input(f'Warm. Remaining guesses {5 - count} '))
elif how_close_to_answer >= 20:
user_input = int(input(f'Cold. Remaining guesses {5 - count} '))
elif how_close_to_answer <= 5:
user_input = int(input(f'Hot. Remaining guesses {5 - count} '))
else:
if how_close_to_answer < last_distance:
if how_close_to_answer <= 5:
user_input = int(input(f'Hotter. Remaining guesses {5 - count} '))
else:
user_input = int(input(f'Warmer. Remaining guesses {5 - count} '))
elif how_close_to_answer > last_distance:
user_input = int(input(f'Colder. Remaining guesses {5 - count} '))
last_distance = how_close_to_answer
if user_input is not 41:
print('You Lose!')
else:
print('You Win!')
print(f"It took you {count + 1} guesses to get this correct.")
Hope this is what helps

Related

print statement only when the condition is CONSECUTIVELY met 3 times in a row

I'm currently making a guessing game where user can get a congrats statement if they guess correctly 3 times in a row or a hint statement if they guesses incorrectly 3 times in a row. If user makes two correct guesses and one incorrect guess the count will reset and vice versa for incorrect guesses. the goal is for the right/wrong guess to be 3 times in a row for the statement to print
Here is what I have
count = 0
rightGuess = 0
wrongGuess = 0
die1 = random.randint(1,6)
guess = int(input('Enter guess: '))
if guess == die1:
rightGuess += 1
print('Good job')
if rightGuess == 3:
print('You guessed three times in a row!')
if guess != die1:
wrongGuess += 1
print('Uh oh wrong answer')
if wrongGuess == 3:
print("Hint: issa number :)")
This works but it displays the text whenever the user reaches 3 wrong or right guesses even if it's not in a row. Please help
You can reset the rightGuess variable using rightGuess = 0 when you add 1 to the wrongGuess variable.
You just have to reset the opposite variable to 0 when incrementing either of them.
count = 0
consecutiveRightGuess = 0
consecutiveWrongGuess = 0
die1 = random.randint(1, 6)
guess = int(input('Enter guess: '))
if guess == die1:
consecutiveWrongGuess = 0
consecutiveRightGuess += 1
print('Good job')
if consecutiveRightGuess == 3:
print('You guessed three times in a row!')
if guess != die1:
consecutiveRightGuess = 0
consecutiveWrongGuess += 1
print('Uh oh wrong answer')
if consecutiveWrongGuess == 3:
print("Hint: issa number :)")
You could also do it like this only using one variable for counting guesses:
import random
count = 0
while True:
die1 = random.randint(1,6)
guess = int(input("Enter guess: "))
if guess == die1:
count = count + 1 if count >= 0 else 1
print('Good job')
if guess != die1:
print('Uh oh wrong answer')
count = count - 1 if count <= 0 else -1
if count == 3:
print('You guessed three times in a row!')
break
if count == -3:
print("Hint: issa number :)")
break

How do I limit it to 5 attempts and how they get 10 points added to their score if they guess correct, and they lose 1 point for wrong guess?

Problem: (Python) Write a program that asks the user to guess a random number between 1 and 10. If they guess right, they get 10 points added to their score, and they lose 1 point for an incorrect guess. Give the user five numbers to guess and print their score after all the guessing is done.
fs: I already have this code below, my problem now is how do I limit it to 5 attempts, also how to add "score system" like they get 10 points added to their score if they guess right, and they lose 1 point for an incorrect guess.
My code:
import random
target_num, guess_num = random.randint(1, 10), 0
while target_num != guess_num:
guess_num = int(input('Guess a number between 1 and 10 until you get it right : '))
print('Well guessed!')
Use attempt variable to track how many times the user has guessed.
Use score variable to track the accumulated score from guessing.
import random
target_num, guess_num = random.randint(1, 10), 0
attempt = 0
score = 0
while attempt < 5:
attempt += 1
guess_num = int(input('Guess a number between 1 and 10: '))
if target_num == guess_num:
score += 10
print('Correct! You get +10 points')
else:
score -= 1
print('Wrong! You get -1 point')
print("Your score: ", score, " points.")
You should do it like this -
import random
target_num, guess_num, tries, score= random.randint(1, 10), 0, 5, 0
for i in range(tries):
guess_num = int(input('Guess a number between 1 and 10 until you get it right : '))
if guess_num == target_num:
print('Well guessed!')
score+=10
else:
score-=1
print(f'Your final score is {score}')

how to add score and loop through game

import random
x = 50
score = 0
number = random.randint(1, x)
[print("This number is divisible by ", str(i)) for i in range(1, 10) if number % i == 0]
print('The largest possible number to guess is ' + str(x))
if number < x/2:
print('This number is less than ' + str(int(x/2)))
else:
print('This number is larger than ' + str(int(x/2)))
print(number)
while True:
if int(input('Guess: ')) == number:
print('You got it')
break
else:
print('Try again!')
What the code does so far is takes a random integer between 1 and whatever number I want. It tells me which numbers it is divisible by between 1-9 and also if it is bigger than half the maximum possible number. It essentially gives you a lot of info to guess.
I want to add a score aspect where after you guess the correct number, you will get 1 added to your score. Then it will loop back to the beginning, get a new number to guess and give all it's information again so you can guess. I'm trying to get the looping part but I'm really lost right now.
When your guess is correct we can add 1 to your current score and print it. You can play till you guess it right. You have to put the whole code in a while loop for looping through the game after every correct answer. You can break the loop if your score is greater than 10 and the game stops.
import random
x = 50
score = 0
while True:
if score >= 10:
break
number = random.randint(1, x)
[print("This number is divisible by ", str(i)) for i in range(1, 10) if number % i == 0]
print('The largest possible number to guess is ' + str(x))
if number < x/2:
print('This number is less than ' + str(int(x/2)))
else:
print('This number is larger than ' + str(int(x/2)))
print(number)
while True:
if int(input('Guess: ')) == number:
print('You got it')
score+=1
print('Your current score',score)
break
else:
print('Try again!')
Haven't worked with Python myself before but I assume you want to encapsulate everything inside a huge while loop and ask at the end of each iteration if you want to keep playing
Something like this (this is more pseudocode than anything, didn't even tested it)
import random
x = 50
score = 0
keepPlaying = True
while keepPlaying:
number = random.randint(1, x)
[print("This number is divisible by ", str(i)) for i in range(1, 10) if number % i == 0]
print('The largest possible number to guess is ' + str(x))
if number < x/2:
print('This number is less than ' + str(int(x/2)))
else:
print('This number is larger than ' + str(int(x/2)))
print(number)
if int(input('Guess: ')) == number:
print('You got it')
score++
break
else:
print('Try again!')
score--
if (input("do want to keep playing?")=="no")
keepPlaying = False

Learning Python - Number game

I'm new to Python. I'm trying to write a small game that asks the end user to pick a number from 1 to 1000 and keep it in their head (the number is not provided to the program). The program should be able to find the number within 10 guesses. As I typically do, I went down the wrong path. My program works most of the time, but there are occasions where it does not find the number in under 10 guesses. Here is my code:
# script to guess a user's number between 1 and 1000 within 10 guesses
# import random so we can use it to generate random numbers
from random import randint
# Variables
lowerBound = 1
upperBound = 1000
numGuesses = 1
myGuess = 500
failed = False
# Welcome Message
print("#####################################################################################################"
"\n# #"
"\n# Please think of a number between 1 and 1000. I will attempt to guess the number in 10 tries. #"
"\n# #"
"\n#####################################################################################################")
while numGuesses <= 10:
# if the lower and upper bounds match we've found the number
if lowerBound == upperBound:
print(f"\nYour number is {str(lowerBound)}. It took me '{str(numGuesses)} guesses!")
break
print(f"\nIs the number {str(myGuess)}? If correct, type CORRECT. If low, type LOW. If high, type HIGH.")
# uncomment for var output
# print(f"\nGuesses = {str(numGuesses)}\nLower bound = {str(lowerBound)}\nUpper bound = {str(upperBound)}")
userFeedback = input("\nResponse: ").upper()
if userFeedback == 'HIGH':
print(f"\nGuess #{str(numGuesses)} was too high")
if numGuesses == 10:
failed = True
break
upperBound = myGuess - 1
myGuess = randint(lowerBound, upperBound)
elif userFeedback == 'LOW':
print(f"\nGuess #{str(numGuesses)} was too low")
if numGuesses == 10:
failed = True
break
lowerBound = myGuess + 1
myGuess = randint(lowerBound, upperBound)
elif userFeedback == 'CORRECT':
print(f"\nYour number is {str(myGuess)}! It took me {str(numGuesses)} guesses!")
break
numGuesses += 1
if failed:
print(f"\nMy final guess of {str(myGuess)} was not correct. I wasn't able to guess your number in 10 tries.")
It seems clear (now) that the way I'm whittling down the numbers is not going to work. I had originally thought to ask if it was 500 and, if lower, ask if it was 250. If lower again, ask if it was 125, and so on. If higher, ask if it was 750, 875 and so on. Is that the correct approach here?
I've been thinking about this too long and I believe I've cooked my brain. Thanks!
myGuess = int(math.ceil((myGuess) / 2))
is not correct.
If you have narrowed down the range to between 6 and 8 and you were guessing 7, your previous code would call 4 instead which is outside your search range.
if userFeedback == 'HIGH':
print(f"\nGuess #{numGuesses} was too high")
upperBound = myGuess - 1
elif userFeedback == 'LOW':
print(f"\nGuess #{numGuesses} was too low")
lowerBound = myGuess + 1
myGuess = int(lowerBound + ((upperBound - lowerBound) / 2))
I've updated my code and I think I have it. Thanks for the tips.
# script to guess a number between 1 and 1000 within 10 guesses
# Variables
lowerBound = 1
upperBound = 1000
numGuesses = 1
myGuess = 500
# Welcome Message
print("#####################################################################################################"
"\n# #"
"\n# Please think of a number between 1 and 1000. I will attempt to guess the number in 10 tries. #"
"\n# #"
"\n#####################################################################################################")
while numGuesses <= 10:
# uncomment next line for var output
# print(f"\nGuesses = {numGuesses}\nLower bound = {lowerBound}\nUpper bound = {upperBound}")
print(f"\nIs the number {myGuess}? If correct, type CORRECT. If low, type LOW. If high, type HIGH.")
userFeedback = input("\nResponse: ").upper()
if userFeedback == 'HIGH':
print(f"\nGuess #{numGuesses} was too high")
upperBound = myGuess
myGuess = (lowerBound + myGuess) // 2
elif userFeedback == 'LOW':
print(f"\nGuess #{numGuesses} was too low")
lowerBound = myGuess
myGuess = (upperBound + myGuess + 1) // 2
elif userFeedback == 'CORRECT':
print(f"\nYour number is {myGuess}! It took me {numGuesses} guesses!")
break
numGuesses += 1

Not able fix variables python

So I'm able to divide the variable I asked earlier (thank you). But I guess I didn't put enough code so here's the whole thing, I'm getting errors all over the place but whatever I change to try to fix it I get the same response. And yes I am new to python and this is due in an hour.
# grade: The student's grade level Ex. 12
# first: 1st six-weeks grade Ex. 98
# second: 2nd six-weeks grade Ex. 78
# third: 3rd six-weeks grade Ex. 89
# num_exemptions: The number of exemptions that the student has already applied for this semester. Ex. 3
# absences: The number of absences that the student has accrued for the semester. Ex. 4
# tardies: The number of tardies that the student has accrued for the semester. Ex. 5
# previously_taken_exemptions: Has the student taken an exemption for the same course in the fall. Ex. True
print('Enter your 1st 6 weeks grade. Ex. 98')
first = input()
print('Enter your 2nd 6 weeks grade. Ex. 78')
second = input()
print('Enter your 3rd 6 weeks grade. Ex. 89')
third = input()
print('Enter the number of exemptions that you have already applied for this semester. Ex. 3')
num_exemptions = input()
print('Enter your grade. Ex. 12')
grade = input()
print('Enter how many absences you have had this semester. Ex. 4')
absences = input()
print('Enter how many times you have been tardy this semester. Ex. 5')
tardies = input()
print('Have you taken and exemption for this course in the fall. Ex. no')
previously_taken_exemptions = input()
real_absences = float(tardies) // 3 + float(absences)
first = int(first)
sum = float(first) + float(second) + float(third)
average = sum/3
if(average >= 81 and average <= 100):
print("Your Grade is an A")
elif(average >= 61 and average <= 80):
print("Your Grade is an B")
elif(average >= 41 and average <= 60):
print("Your Grade is an C")
elif(average >= 0 and average <= 40):
print("Your Grade is an F")
else:
print("You did something wrong, try again")
if float(grade == '11') and float(num_exemptions) <= 2 and float(real_absences) <= 3 and float(previously_taken_exemptions) == 'no' and float(average) >= 84:
print('You are eligable!')
elif float(grade == '12') and float(num_exemptions) <= 4 and float(real_absences) <= 3 and float(previously_taken_exemptions) == 'no' and float(average) >= 84:
print('You are eligable!')
elif float(grade == '9' or '10') and float(num_exemptions) <= 1 and float(real_absences) <= 3 and float(previously_taken_exemptions) == 'no' and float(average) >= 84:
print('You are eligable!')
else:
print('You are not eligable')
**
It's because it's a string, use:
real_absences = float(tardies) // 3 + float(absences)
You can turn your strings into integers using int.
you have to just declare integer.
here is an example
Tardies = int(“15”)
Absences = int(“5”)
real_absences = Tardies // 3 + Absences

Categories