user id/if statements

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

user id/if statements

Postby mckryall » Sat Nov 23, 2013 6:21 pm

I'm having a problem with making a user id and asking the question again/redefining the variable if it's 'wrong'. I didn't want to have to copy and paste the code into the elif box and make it nest for too long, so I decided to make it into a function. Here's the function.

Code: Select all
def id():
   
   id = raw_input("> ")
   if id < 1000
      print "Too low. Try again.
   elif id > 9999
      print "Too high. Try again."
   else
      print "Good."


python says "if id < 1000" is wrong. What should I change, and should I even be using functions? is there some way to have something like this:
elif id < x
(ask raw input again and then go through if statement again)

Thanks
Last edited by stranac on Sat Nov 23, 2013 6:26 pm, edited 1 time in total.
Reason: Added code tags. Locked.
Python 2.7
Mac Mini
Laptop w/ Ubuntu 14.04
Steam; PSN
mckryall
 
Posts: 94
Joined: Sat Nov 23, 2013 6:13 pm

Re: user id/if statements

Postby stranac » Sat Nov 23, 2013 6:28 pm

You're missing colons. It should be:
Code: Select all
if id < 1000:

Also, id is the name of a built-in function. You should not be using it as a name for your variables.

Please read this before posting again.
Friendship is magic!

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

Re: user id/if statements

Postby mckryall » Sat Nov 23, 2013 6:30 pm

Huh. I keep forgetting about that. There are only a couple places they're used in the entire coding language.
Last edited by Mekire on Sat Nov 30, 2013 7:41 am, edited 1 time in total.
Reason: Locked.
Python 2.7
Mac Mini
Laptop w/ Ubuntu 14.04
Steam; PSN
mckryall
 
Posts: 94
Joined: Sat Nov 23, 2013 6:13 pm

Re: user id/if statements

Postby micseydel » Sat Nov 23, 2013 7:02 pm

I think your problem here is that raw_input() gives you a string, which you're comparing to an integer. You need to use int() on the string.
Code: Select all
>>> num = raw_input("Enter a number> ")
Enter a number> 7
>>> num < 8
False
>>> int(num) < 8
True

The typical way I'm used to people handling this is to do
Code: Select all
num = int(raw_input("Enter a number> "))

unless you need the string representation somewhere.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1369
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: user id/if statements

Postby mckryall » Sat Nov 23, 2013 7:23 pm

Thanks. I didn't catch that. Now it works.
Last edited by Mekire on Sat Nov 30, 2013 7:41 am, edited 1 time in total.
Reason: Locked.
Python 2.7
Mac Mini
Laptop w/ Ubuntu 14.04
Steam; PSN
mckryall
 
Posts: 94
Joined: Sat Nov 23, 2013 6:13 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 2 guests