Login Register


Legends Python Challenge #2: Guessing The Number filter_list
Author
Message
RE: Python Challenge #2: Guessing The Number #5
Code:
import random def g(): r = random.randint(1,100) gc = 0 g = int(raw_input("Guess a number between 1 and 100: ")) while g != r: gc+=1 if g > r: g = int(raw_input("Try going lower, guess a number between 1 and 100: ")) if g < r: g = int(raw_input("Try going higher, guess a number between 1 and 100: ")) if g == r: gc+=1 gc = str(gc) r = str(r) print('You guessed the number '+r+' in '+gc+' guesses.') def a(): a = raw_input("Would you like to play again? [y/n] ") if a == "y": g() g() a()

(12-16-2015, 08:04 PM)God Wrote: My reg version:
Spoiler:
Code:
import random def guess_check(): random_number = random.randint(1, 100) guesses = 0 guess = int(input('Guess the number: ')) while guess != random_number: guesses+=1 if guess > random_number: print('Too high, try again. ') guess = int(input('Guess the number: ')) elif guess < random_number: print('Too low, try again. ') guess = int(input('Guess the number: ')) if guess == random_number: guesses+=1 print('Congratulations! You guessed the number in',guesses, 'guesses.') def start_over(): play_again = input('Do you want to play again? (Y or N) ') while play_again.lower() == 'y': guess_check()

The only thing your code does right there is define functions. Nothing is actually running.
Either way, when someone guesses the number, it prints
Code:
('Congratulations! You guessed the number in', x, 'guesses.')
You can fix that by converting "guesses" to a string then concatenating it normally.

Also, if someone enters N or Y whenever the script asks if they want to play again, it'll just throw them an error. That's how it behaves in command prompt at least.
(This post was last modified: 12-17-2015, 04:28 AM by meow.)

[+] 1 user Likes meow's post
Reply





Messages In This Thread
Python Challenge #2: Guessing The Number - by Nil - 12-16-2015, 08:04 PM
RE: Python Challenge #2: Guessing The Number - by meow - 12-17-2015, 04:23 AM



Users browsing this thread: 1 Guest(s)