Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Need help with my if / elsif !
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
m84uily




PostPosted: Fri Jan 07, 2011 4:07 am   Post subject: Need help with my if / elsif !

I'm just starting out with Python and I can't figure out why I'm getting a syntax error on my elif. Help would be very much appreciated, thanks in advance.

code:

# Phonebook maker 1.0!
phonebook = {}
def Book_Maker(a,b):
    t = 0
    try:
        y = str(a)
        x = int(b)
    except:
        t = 1
    if t != 1:
        phonebook[ a ] = b
    else:
        print "You did something wrong, oops!"
loop = 1
answer = 0
while loop == 1:
    print "If you would like to add a person to your phonebook hit 1. If you would like to see your phonebook enter 2."
    answer = input("Input 1 or 2 for your answer: ")
    if answer == 1:
        Book_Maker(raw_input("Enter the person's name: "), raw_input("Enter the person's number: ")
    elif answer == 2:
        print phonebook.keys(), print phonebook.values()
    else:
        print "Please enter either 1 or 2."
Sponsor
Sponsor
Sponsor
sponsor
m84uily




PostPosted: Fri Jan 07, 2011 4:36 am   Post subject: Re: Need help with my if / elsif !

code:


# Phonebook maker 1.0!
phonebook = {}
loop = 1
answer = 0
while loop == 1:
    print "If you would like to add a person to your phonebook hit 1. If you would like to see your phonebook enter 2."
    answer = input("Input 1 or 2 for your answer: ")
    if answer == 1:
        a = raw_input("Enter the person's name: ")
        b = raw_input("Enter the person's number: ")
        if a != str or b != int:
            print "You did something wrong! Try again."
        else:
            phonebook[ a ] = b
    elif answer == 2:
        print phonebook
    else:
        print "Please enter either 1 or 2."
   


Did this instead and it works fine, decided to just give up on the function xD.
rdrake




PostPosted: Fri Jan 07, 2011 6:20 am   Post subject: Re: Need help with my if / elsif !

It wasn't your elif, it was what you had inside that was causing issues. You cannot do this.

Python:
print "foo", print "bar"


Also lose the input(), it's dangerous. Just use raw_input() and treat the potential choices as strings.

Just because I can:

Python:
class Phonebook:
        def __init__(self):
                self.phonebook = {}
       
        def add(self, name, number):
                try:
                        name = str(name)
                        number = int(number)
                except:
                        raise ValueError("You did something wrong, oops!")

                self.phonebook[name] = number
       
        def __str__(self):
                s = ""

                for name, number in self.phonebook.items():
                        s += "%s : %d" % (name, number)

                return s

if __name__ == "__main__":
        p = Phonebook()

        while True:
                print "If you would like to add a person to your phonebook hit 1. If you would like to see your phonebook enter 2."
                answer = raw_input("Input 1 or 2 for your answer: ")

                if answer == "1":
                        p.add(raw_input("Enter the person's name: "), raw_input("Enter the person's number: "))
                elif answer == "2":
                        print p
                else:
                        print "Please enter either 1 or 2."


Though it's not quite as forgiving as yours is, it's easy to fix if you really want.

code:
If you would like to add a person to your phonebook hit 1. If you would like to see your phonebook enter 2.
Input 1 or 2 for your answer: 1
Enter the person's name: John Doe
Enter the person's number: 5551234
If you would like to add a person to your phonebook hit 1. If you would like to see your phonebook enter 2.
Input 1 or 2 for your answer: 2
John Doe : 5551234
If you would like to add a person to your phonebook hit 1. If you would like to see your phonebook enter 2.
Input 1 or 2 for your answer: 1
Enter the person's name: John Smith
Enter the person's number: NaN
Traceback (most recent call last):
  File "test.py", line 30, in <module>
    p.add(raw_input("Enter the person's name: "), raw_input("Enter the person's number: "))
  File "test.py", line 10, in add
    raise ValueError("You did something wrong, oops!")
ValueError: You did something wrong, oops!
m84uily




PostPosted: Fri Jan 07, 2011 12:06 pm   Post subject: Re: Need help with my if / elsif !

rdrake @ Fri Jan 07, 2011 6:20 am wrote:
It wasn't your elif, it was what you had inside that was causing issues. You cannot do this.

Python:
print "foo", print "bar"


Also lose the input(), it's dangerous. Just use raw_input() and treat the potential choices as strings.

Just because I can:

Python:
class Phonebook:
        def __init__(self):
                self.phonebook = {}
       
        def add(self, name, number):
                try:
                        name = str(name)
                        number = int(number)
                except:
                        raise ValueError("You did something wrong, oops!")

                self.phonebook[name] = number
       
        def __str__(self):
                s = ""

                for name, number in self.phonebook.items():
                        s += "%s : %d" % (name, number)

                return s

if __name__ == "__main__":
        p = Phonebook()

        while True:
                print "If you would like to add a person to your phonebook hit 1. If you would like to see your phonebook enter 2."
                answer = raw_input("Input 1 or 2 for your answer: ")

                if answer == "1":
                        p.add(raw_input("Enter the person's name: "), raw_input("Enter the person's number: "))
                elif answer == "2":
                        print p
                else:
                        print "Please enter either 1 or 2."


Though it's not quite as forgiving as yours is, it's easy to fix if you really want.

code:
If you would like to add a person to your phonebook hit 1. If you would like to see your phonebook enter 2.
Input 1 or 2 for your answer: 1
Enter the person's name: John Doe
Enter the person's number: 5551234
If you would like to add a person to your phonebook hit 1. If you would like to see your phonebook enter 2.
Input 1 or 2 for your answer: 2
John Doe : 5551234
If you would like to add a person to your phonebook hit 1. If you would like to see your phonebook enter 2.
Input 1 or 2 for your answer: 1
Enter the person's name: John Smith
Enter the person's number: NaN
Traceback (most recent call last):
  File "test.py", line 30, in <module>
    p.add(raw_input("Enter the person's name: "), raw_input("Enter the person's number: "))
  File "test.py", line 10, in add
    raise ValueError("You did something wrong, oops!")
ValueError: You did something wrong, oops!


Thanks a lot! Why is input() dangerous, though?
DemonWasp




PostPosted: Fri Jan 07, 2011 1:36 pm   Post subject: RE:Need help with my if / elsif !

The documentation on input() is here: http://docs.python.org/library/functions.html#input

The reason it's dangerous is that it lets you enter scripts. The user could input any Python expression, which can be pretty well whatever they want. I'm sure someone could figure out a relatively easy way to use this to delete files, download something from the internet and run it, or whatever else.
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: