looking for advice for coding project - python

So as of right now the code needs to re-ask the problem given if the answer given was too high or too low. Also, if the answer is correct, it should tell them and then loop back to the question ('How many problems do you want?')
def main():
gamenumber = int(input("How many problems do you want?\n"))
count = 0
while count < gamenumber:
num_1 = randint(1,10)
num_2 = randint(1,10)
guess = int(input("What is " + str(num_1) + "x" + str(num_2) + "."))
answer = (num_1*num_2)
count += 1
for guess in range(1):
if guess == answer:
print (' Thats Correct!')
for guess in range(1):
if guess > answer:
print (' Answer is to high')
for guess in range(1):
if guess < answer:
print ('Answer is to low')
main()

First of all can you please check your code. You have used "guess" variable in the for loop. When the program is executed the value of guess is say 40(4X10). When for statement is executed the guess values becomes 0 because of that you are getting the output as low. Make sure u change the variable you use in for loop to "num" and then check your output.
Why are you using 3 for loops you can do that in single for loop.
Please find the below code:-
from random import randint
def main():
ans = 'y'
while ans != 'n':
gamenumber = int(input("How many problems do you want?\n"))
count = 0
while count < gamenumber:
num_1 = randint(1,10)
num_2 = randint(1,10)
guess = int(input("What is " + str(num_1) + "x" + str(num_2) + "."))
print(guess)
answer = (num_1*num_2)
print("=====>",answer)
count += 1
for num in range(1):
if guess == answer:
print (' Thats Correct!')
elif guess > answer:
print (' Answer is to high')
elif guess < answer:
print ('Answer is to low')
yes_no_input = str(input("Do you want to continue (y/n) ?"))
ans = accept_ans(yes_no_input)
if ans == 'n':
print("thanks for the test")
break;
def accept_ans(ans):
if not ans.isalpha():
print("Enter only y and n")
ans = str(input("Do you want to continue (y/n) ?"))
if ans == 'y' or ans == 'n':
return ans
if ans != 'y' or ans != 'n':
print("please enter y for YES and n for NO")
ans = str(input("Do you want to continue (y/n) ?"))
if ans != 'y' or ans != 'n':
accept_ans(ans)
if __name__ == '__main__':
main()

After
print("Thats correct")
you need to call the
main()
function again.

Related

Is it possible this way? Letting computer to guess my number

I want pc to find the number in my mind with random function "every time"
so i tried something but not sure this is the correct direction and its not working as intended
how am i supposed to tell pc to guess higher than the previous guess
ps:code is not complete just wondering is it possible to do this way
def computer_guess():
myguess = int(input("Please enter your guess: "))
pcguess = randint(1, 10)
feedback = ""
print(pcguess)
while myguess != pcguess:
if myguess > pcguess:
feedback = input("was i close?: ")
return feedback, myguess
while feedback == "go higher":
x = pcguess
pcguess2 = randint(x, myguess)
print(pcguess2)
I wrote this a few years ago and it's similar to what you're trying to do except this only asks for user input once then calls random and shrinks the range for the next guess until the computer guess is equal to the user input.
import random
low = 0
high = 100
n = int(input(f'Chose a number between {low} and {high}:'))
count = 0
guess = random.randint(0,100)
lower_guess = low
upper_guess = high
while n != "guess":
count +=1
if guess < n:
lower_guess = guess+1
print(guess)
print("is low")
next_guess = random.randint(lower_guess , upper_guess)
guess = next_guess
elif guess > n:
upper_guess = guess-1
print(guess)
print("is high")
next_guess = random.randint(lower_guess , upper_guess)
guess = next_guess
else:
print("it is " + str(guess) + '. I guessed it in ' + str(count) + ' attempts')
break

Number Guessing Games with continuation

I know there is a solution to the number guessing game in python. I've seen the code here too. and as a continuation how can I add a function(or anything else) to prompt the user- after successfully guessing it- if he wants to continue or not if not halt it. I know the code don't know how to add it to code.
import random
def play_game():
print("Enter the upper limit for the range of numbers: ")
limit = int(input())
number = random.randint(1, limit)
print("I'm thinking of a number from 1 to " + str(limit) + "\n")
count = 1
while True:
guess = int(input("Your guess: "))
if guess < number:
print("Too low.")
elif guess > number:
print("Too high.")
elif guess == number:
print("You guessed it in " + str(count) + " tries.\n")
return
count+=1
so here is what we had in this forum but how can I add this to what I have above :
ask = input('do you want to continue? y stands for yes and n for discontinuing')
if ask=="y":
UserGuess=int(input('Enter youre desired Number: '))
else:
return
Include it in a while loop outside the function. This way, you will get recursion out of the way.
def play_game():
....
....
count+=1
play_game()
while True:
ask = input('do you want to continue? y stands for yes and n for discontinuing')
if ask=="y":
play_game()
else:
break

python guess game: help the player on the correct answer

hellow, I'm confused about writing this exercise from my book, can you please help me.
So far I have managed to write this amount:
import random
def main():
again = 'y'
number_of_guesses = 0
while again =='y':
print('guess the number game')
print(' ')
print('Guess my number between 1 and 1000 with the fewest guesses: ')
x = random.randrange(1,1000)
guess = int(input())
while guess !='':
if guess < x:
print('Your guess is too low , try again')
guess = int(input())
elif guess > x:
print('Your guess is too high , try again')
guess = int(input())
elif guess == x:
print('Congratulations! You guessed the number')
print('Do you want to repeat?')
print('if you want do, type y, else type the n')
number_of_guesses += 1
again = str(input(''))
break
else:
print('Error/!\, your enter is wrong:')
main()
The main text of the question: as appropriate to help the player “zero in” on the correct answer, then prompt the user for the next guess. When the user enters
the correct answer, display "Congratulations. You guessed the number!", and allow the
user to choose whether to play again.
your code is all ok! What's the deal? Check the indentations if still wrong
I think you already solve the problem, where exactly you need help?
And instead of putting
while guess !+ '':
I would put
while True:
You have only multiple indentation errors. Fixed version would be as follows:
import random
def main():
again = 'y'
number_of_guesses = 0
while again =='y':
print('guess the number game')
print(' ')
print('Guess my number between 1 and 1000 with the fewest guesses: ')
x = random.randrange(1,1000)
guess = int(input())
while guess !='':
if guess < x:
print('Your guess is too low , try again')
guess = int(input())
elif guess > x:
print('Your guess is too high , try again')
guess = int(input())
elif guess == x:
print('Congratulations! You guessed the number')
print('Do you want to repeat?')
print('if you want do, type y, else type the n')
number_of_guesses += 1
again = str(input(''))
break
else:
print('Error/!\, your enter is wrong:')
main()

Computer guessing number python

I'm very new to programming and am starting off with python. I was tasked to create a random number guessing game. The idea is to have the computer guesses the user's input number. Though I'm having a bit of trouble getting the program to recognize that it has found the number. Here's my code and if you can help that'd be great! The program right now is only printing random numbers and won't stop even if the right number is printed that is the problem
import random
tries = 1
guessNum = random.randint(1, 100)
realNum = int(input("Input a number from 1 to 100 for the computer to guess: "))
print("Is the number " + str(guessNum) + "?")
answer = input("Type yes, or no: ")
answerLower = answer.lower()
if answerLower == 'yes':
if guessNum == realNum:
print("Seems like I got it in " + str(tries) + " try!")
else:
print("Wait I got it wrong though, I guessed " + str(guessNum) + " and your number was " + str(realNum) + ", so that means I'm acutally wrong." )
else:
print("Is the number higher or lower than " + str(guessNum))
lowOr = input("Type in lower or higher: ")
lowOrlower = lowOr.lower()
import random
guessNum2 = random.randint(guessNum, 100)
import random
guessNum3 = random.randint(1, guessNum)
while realNum != guessNum2 or guessNum3:
if lowOr == 'higher':
tries += 1
import random
guessNum2 = random.randint(guessNum, 100)
print(str(guessNum2))
input()
else:
tries += 1
import random
guessNum3 = random.randint(1, guessNum)
print(str(guessNum3))
input()
print("I got it!")
input()
How about something along the lines of:
import random
realnum = int(input('PICK PROMPT\n'))
narrowguess = random.randint(1,100)
if narrowguess == realnum:
print('CORRECT')
exit(1)
print(narrowguess)
highorlow = input('Higher or Lower Prompt\n')
if highorlow == 'higher':
while True:
try:
guess = random.randint(narrowguess,100)
print(guess)
while realnum != guess:
guess = random.randint(narrowguess,100)
print(guess)
input()
print(guess)
print('Got It!')
break
except:
raise
elif highorlow == 'lower':
while True:
try:
guess = random.randint(1,narrowguess)
print(guess)
while realnum != guess:
guess = random.randint(1,narrowguess)
print(guess)
input()
print(guess)
print('Got It!')
break
except:
raise
This code is just a skeleton, add all of your details to it however you like.

There's an issue somewhere in my Python code.. I can't find where it's at

I don't know what's wrong with it.. I run it and I'm able to input a number but then it stops working. It says, "TypeError: play_game() missing 1 required positional argument: 'limit.' But I'm not sure what's missing there??
#!/usr/bin/env python3
import random
def display_title():
print("Guess the number!")
print()
def get_limit():
limit = int(input("Enter the upper limit for the range of numbers: "))
return limit
def play_game(limit):
number = random.randint(1, limit)
print("I'm thinking of a number from 1 to " + str(limit) + "\n")
while True:
guess = int(input("Your guess: "))
if guess < number:
print("Too low.")
count += 1
elif guess >= number:
print("Too high.")
count += 1
elif guess == number:
print("You guessed it in " + str(count) + " tries.\n")
return
def main():
display_title()
again = "y"
while again.lower() == "y":
limit = get_limit()
play_game()
again = input("Play again? (y/n): ")
print()
print("Bye!")
# if started as the main module, call the main function
if __name__ == "__main__":
main()
You have defined your play_game function to take limit as a parameter, but when you call this function in your main loop, you don't supply a value in the brackets of play_game().
You could either try adding that limit value that you've specified by calling it like
play_game(25)
Or, based on your code, since you're asking the user to provide a limit, call it like:
play_game(limit)
Or, if you want to be able to call play_game() without setting a limit, then change your play_game definition line to something like:
def play_game(limit=25):
Which will set a default value of 25 whenever that function is called without supplying the limit value.
Yes, play_game() needs the parameter limit. I've done a quick check on your code, and there is some additional problem
the count variable isn't initialized
you calculate the random number in every step
guess > number should be used instead of guess >= number
Here is the fixed code, it works for me. I hope it will be usefull:
import random
count = 0
number = -1
def display_title():
print("Guess the number!")
print()
def get_limit():
limit = int(input("Enter the upper limit for the range of numbers: "))
return limit
def play_game(limit):
global number, count
if number == -1:
number = random.randint(1, limit)
print("I'm thinking of a number from 1 to " + str(limit) + "\n")
while True:
guess = int(input("Your guess: "))
if guess < number:
print("Too low.")
count += 1
elif guess > number:
print("Too high.")
count += 1
elif guess == number:
print("You guessed it in " + str(count) + " tries.\n")
return
display_title()
again = "y"
while again.lower() == "y":
limit = get_limit()
play_game(limit)
again = input("Play again? (y/n): ")
print()
print("Bye!")
In your main you are calling playgame() without providing a limit as an argument.
Your main should look something like
def main():
display_title()
again = "y"
while again.lower() == "y":
limit = get_limit()
play_game(10)
again = input("Play again? (y/n): ")
print()
print("Bye!")

Categories