![]() |
Guess a number - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Guess a number (/thread-9330.html) |
Guess a number - Ameen - Apr-02-2018 Hello I a beginner trying to practice after completing the codeacademy course. Right now im trying to do a guess a number game. This is the instructions The Goal: Similar to the first project, this project also uses the random module in Python. The program will first randomly generate a number unknown to the user. The user needs to guess what that number is. (In other words, the user needs to be able to input information.) If the user’s guess is wrong, the program should return some sort of indication as to how wrong (e.g. The number is too high or too low). If the user guesses correctly, a positive indication should appear. You’ll need functions to check if the user input is an actual number, to see the difference between the inputted number and the randomly generated numbers, and to then compare the numbers. This is my code from random import randint def guess(x): it = randint(0, 101) if x == it: print("You got it!") elif x > it: print("too high") else: print("too low") print guess(50)The problem with it is I only get one chance to guess. I think i need to use a while statement and some sort of +- symbol, but it kept turning out wrong. RE: Guess a number - ljmetzger - Apr-02-2018 Welcome to the Python forum, A small script like yours does not have to be a function. See the following link for use of a while loop. http://www.pythonforbeginners.com/control-flow-2/python-for-and-while-loops Seed the value outside of the loop, and then loop until an answer is obtained. Use an input() statement to get the guess from the user. Additional items to consider after you are successful: a. What if the user types in something other than a number? b. What if the user wants to give up? Please let us know if you need additional help. Lewis RE: Guess a number - Ares - Apr-03-2018 This is a fixed edit of your code import random it=random.randint(0, 101) def main(): x=int(input('Guess a number one through one hundred')) if x == it: print("You got it!") elif x > it: print("too high") main() else: print("too low") main() main() RE: Guess a number - scidam - Apr-03-2018 Recursive invocation is limited by depth. count = 0 def main(): global count count += 1 return count main()This code leads to So, the program can't produce too much queries (but this behavior (recursion depth) could be tweaked).Using infinite loop is more preferable in this case. from random import randint it = randint(0, 101) def guess(): while True: x = int(input('Guess a number one through one hundred: ')) if x == it: print("You got it!") break elif x > it: print("too high") else: print("too low") guess() RE: Guess a number - sylas - Apr-03-2018 here a more sophisicated one: import random print("gues_number.py") print("GUESS NUMBER >1 AND <100 ") answer = random.randint(1, 99) print(answer) maximum = 100 maximum2 = 100 minimum = 1 new_min = 1 new_max2 = 100 print(minimum, "<your number<", maximum) while True: guess = int(input()) if guess == answer: print("bingo answer is", answer) break elif guess < answer: print("wrong answer, guess larger") new_max = guess new_min = guess print(guess, "<?<", new_max2) else: answer < guess print("wrong answer, guess smaller") new_max2 = guess new_min2 = guess print(new_min, "<?<", guess) input("Press ENTER to continue for another guess file") secret_number = 34 tries, guess = 0, 0 while guess != secret_number: guess = int(input("Guess a number (<100): ")) if guess > secret_number: print("Lower...") elif guess < secret_number: print("Higher...") tries += 1 print('You guessed it! The number was {} in {} tries'.format(guess, tries)) #input("Press ENTER to continue") RE: Guess a number - snippsat - Apr-03-2018 Hmm that's a two code put together @sylas,i do recognize a demo i did in a post before secret_number = 34 ![]() But the first part should not be there,it's a different code. Here how my code should look,as you just copy my hint in the other post.So the they point here is that there no break,code run as long as not the secret number is guessed. import random secret_number = random.randint(1, 100) tries, guess = 0, 0 while guess != secret_number: guess = int(input("Guess a number (<100): ")) if guess > secret_number: print("Lower...") elif guess < secret_number: print("Higher...") tries += 1 print('You guessed it! The number was {} in {} tries'.format(guess, tries))
|