New to python, need help

This is the place for queries that don't fit in any of the other categories.

New to python, need help

Postby lehongnhat314 » Sat Oct 19, 2013 11:56 pm

Hi, I'm having some problem with my code. Can you guys check it out for me? Thanks
Code: Select all
guess=int(input('Enter number'))
times=0
while guess > end or guess < 1:
   print('Enter again')
   guess=int(input(' '))
while guess > num:
   if guess - num < 10:
      print('Too high! Guess again.')
   else:
      print('Way too high! Guess again.')
   guess=int(input('Enter number'))
   times=times+1
while guess < num:
   if num - guess <10:
      print('Too low! Guess again')
   else:
      print('Way too low! Guess again.')
   guess=int(input('Enter number'))
   times=times+1
if guess == num:
   print('You got it in', times, 'tries')

It sometime works just fine, but sometimes it just break out of the loop even if i haven't got the right guess.
Last edited by stranac on Sun Oct 20, 2013 12:08 am, edited 2 times in total.
Reason: First post lock + fixed indentation
lehongnhat314
 
Posts: 7
Joined: Sat Oct 19, 2013 11:52 pm

Re: New to python, need help

Postby stranac » Sun Oct 20, 2013 12:20 am

Yeah, that doesn't do what you want.

This code first makes sure that the user's input is not too big, and then checks that a different input is not too low.
But it does not check that this new input is not too big.

You will probably need to put the entire thing inside a loop, and do the checking with simple conditionals.
Something like(pseudo code):
Code: Select all
while not correct:
    get the guess
    if guess is to big:
        print 'too big'
    elif guess is too low:
        print too low
    else:
        it has to be correct now
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: New to python, need help

Postby lehongnhat314 » Mon Oct 21, 2013 12:27 am

Thank you, got it working xD
lehongnhat314
 
Posts: 7
Joined: Sat Oct 19, 2013 11:52 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests