You are not logged in.

#1 2011-06-20 02:02:17

duke11235
Member
Registered: 2009-10-09
Posts: 221

[SOLVED]Python3 If/Else Loop (Noob Question)

I'm learning python because of it's ease of use and mathematical libraries, and I decided to create a program to mess with loops.

Here is the program:

#!/usr/bin/python
#Filename: evil.py
print("*******************************************************************")
print("*                             Gorge of Eternal Peril Challenge                                  *")
print("*                                                                                                                  *")
print("*******************************************************************")
print()
print()
ca2 = 'Blue'
ca3 = 'African or European Swallow?'
a1 = input('What...  is Your Name?\n')
if a1 != int:
    a2 = input('What...  is Your Favorite Color?\n')
    if a2 == ca2:
        a3 = input('What...  is The Airspeed Velocity of an Unladen \
Swallow?\n')
         if a3 == ca3:
             print(" I don't know that! AAAAAUUUUUUGGGHHHH...")
                    
else:
    print('AAAAAUUUUUUGGGHHHH You have been cast in to the Gorge of \
Eternal Peril!')

I'm not sure why, but it won't end up in the else statement. Also, if anyone can tell me how to exclude numbers from question one I'd be much obliged.
Thanks.

EDIT: Some strange things happened in this window. The Title box is lined up and the if statements are at the same tab

Last edited by duke11235 (2011-06-20 04:12:46)

Offline

#2 2011-06-20 02:11:03

Guff
Member
Registered: 2009-09-30
Posts: 158
Website

Re: [SOLVED]Python3 If/Else Loop (Noob Question)

With

if a1 != int:

you're comparing the value of a1 against the type int. Because a1 isn't equal to the type int, the else is never reached.

To do what I think you want to do, you should do

if type(a1) is not int:

instead.

Offline

#3 2011-06-20 02:17:11

markbabc
Member
Registered: 2010-11-06
Posts: 157

Re: [SOLVED]Python3 If/Else Loop (Noob Question)

Guff wrote:

With

if a1 != int:

you're comparing the value of a1 against the type int. Because a1 isn't equal to the type int, the else is never reached.

To do what I think you want to do, you should do

if type(a1) is not int:

instead.

Im a Python coder and iv recently been told that when comparing types you should always use the logical "not" "is" instead of "==" and "!=" do you know why this would be?

Offline

#4 2011-06-20 02:18:45

duke11235
Member
Registered: 2009-10-09
Posts: 221

Re: [SOLVED]Python3 If/Else Loop (Noob Question)

Wouldn't that fail with floating point numbers? I guess you might be able to just add an or. Strangely replacing

 if a1 != int:

with

 if type(a1) is not int:

or

 if type(a1) is not int or float or long or complex:

continues through the ifs if i enter something like 1 or 3.

But that seems closer to the answer. Perhaps input() stores everything as a string? But I don't really know

Last edited by duke11235 (2011-06-20 02:25:54)

Offline

#5 2011-06-20 02:32:06

Guff
Member
Registered: 2009-09-30
Posts: 158
Website

Re: [SOLVED]Python3 If/Else Loop (Noob Question)

duke11235 wrote:

But that seems closer to the answer. Perhaps input() stores everything as a string? But I don't really know

It's been a while, but yes, I think in python3 input always returns a string. In python2, input() would evaluate the input as a python expression.

markbabc wrote:

Im a Python coder and iv recently been told that when comparing types you should always use the logical "not" "is" instead of "==" and "!=" do you know why this would be?

Again, it's been a while, and I don't exactly remember why. The difference, though, is that "is" tests whether two things are exactly the same object, whereas "==" calls a method to see if the two are equal. I can't remember why that's important, however.

Offline

#6 2011-06-20 02:35:30

duke11235
Member
Registered: 2009-10-09
Posts: 221

Re: [SOLVED]Python3 If/Else Loop (Noob Question)

So how could I evaluate that first question to exclude numbers if it's a string? And I'm still looking for an explanation on the correct use of else for this script. I've edited the title to indicate python 3 though.  I did run in to a raw_input v input problem in the making though. Thanks

Last edited by duke11235 (2011-06-20 02:37:19)

Offline

#7 2011-06-20 02:45:33

markbabc
Member
Registered: 2010-11-06
Posts: 157

Re: [SOLVED]Python3 If/Else Loop (Noob Question)

#!/usr/bin/python
#Filename: evil.py
print("*******************************************************************")
print("*                             Gorge of Eternal Peril Challenge                                  *")
print("*                                                                                                                  *")
print("*******************************************************************")
print()
print()
ca2 = 'Blue'
ca3 = 'African or European Swallow?'
a1 = input('What...  is Your Name?\n')
if a1 != int:
    a2 = input('What...  is Your Favorite Color?\n')
    if a2 == ca2:
        a3 = input('What...  is The Airspeed Velocity of an Unladen \
Swallow?\n')
         if a3 == ca3:
             print(" I don't know that! AAAAAUUUUUUGGGHHHH...")
                    
else:
    print('AAAAAUUUUUUGGGHHHH You have been cast in to the Gorge of \
Eternal Peril!')

do it like this:

#!/usr/bin/python
#Filename: evil.py
print("*******************************************************************")
print("*                             Gorge of Eternal Peril Challenge                                  *")
print("*                                                                                                                  *")
print("*******************************************************************")
print()
print()
def questions():
    ca2 = 'Blue'
    ca3 = 'African or European Swallow?'
    a1 = input('What...  is Your Name?\n')
    if type(a1) is str:
        a2 = input('What...  is Your Favorite Color?\n')
        if a2 == ca2:
            a3 = input('What...  is The Airspeed Velocity of an Unladen \
Swallow?\n')
             if a3 == ca3:
                 print(" I don't know that! AAAAAUUUUUUGGGHHHH...")
                 return true
             else: return false # Else of if a3 == ca3
        else: return fase # Else of if a2 == ca2
    else: return false # Else of if type(a1) statement
                    
    
if questions(): pass
else: print('AAAAAUUUUUUGGGHHHH You have been cast in to the Gorge of \
Eternal Peril!')

Last edited by markbabc (2011-06-20 02:48:07)

Offline

#8 2011-06-20 03:00:43

duke11235
Member
Registered: 2009-10-09
Posts: 221

Re: [SOLVED]Python3 If/Else Loop (Noob Question)

This prints the title and then exits, I'm not sure of the cause however. There is a missing l in one of the else false statements, but that's not it.

Thanks for the structure info though, 
What does

 if questions: pass

do?

EDIT: Forgot to call the function.  It gives global name true(and false) is not defined, but I am not sure how to define it. The first question still accepts numerical input. I suppose I could just change that to a defined name, and I probably will but I am  curious of how number exclusion could be accomplished with input(). Thanks for the help

Last edited by duke11235 (2011-06-20 03:24:22)

Offline

#9 2011-06-20 03:31:25

markbabc
Member
Registered: 2010-11-06
Posts: 157

Re: [SOLVED]Python3 If/Else Loop (Noob Question)

duke11235 wrote:

This prints the title and then exits, I'm not sure of the cause however. There is a missing l in one of the else false statements, but that's not it.

Thanks for the structure info though, 
What does

 if questions: pass

do?

EDIT: Forgot to call the function.  It gives global name true(and false) is not defined, but I am not sure how to define it. The first question still accepts numerical input. I suppose I could just change that to a defined name, and I probably will but I am  curious of how number exclusion could be accomplished with input(). Thanks for the help

I edited it to fix my mistake, it should have been questions() not questions

EDIT: Also look at str.isalpha() and str.isdigit() could be another way to sanitize the inputs

Last edited by markbabc (2011-06-20 03:36:06)

Offline

#10 2011-06-20 03:44:33

duke11235
Member
Registered: 2009-10-09
Posts: 221

Re: [SOLVED]Python3 If/Else Loop (Noob Question)

What about the undefined global variables true and false? Could it be a difference between python2 and python3? I'll look at those to sanitize input. Thanks

EDIT: Perhaps if they are capitalized? Then you could change the pass statements to boolean statements about the state of the function.

EDIT2: I have a working script, but I had to import sys to stop the program from repeating the function once called. Thanks.

Last edited by duke11235 (2011-06-20 04:03:51)

Offline

#11 2011-06-20 04:02:00

markbabc
Member
Registered: 2010-11-06
Posts: 157

Re: [SOLVED]Python3 If/Else Loop (Noob Question)

duke11235 wrote:

What about the undefined global variables true and false? Could it be a difference between python2 and python3? I'll look at those to sanitize input. Thanks

EDIT: Perhaps if they are capitalized? Then you could change the pass statements to boolean statements about the state of the function.

EDIT2: I have successfully done so, but I had to import sys to break the function from a loop. Thanks. Tomorrow, I'll look in to


My bad in Python true and false are capitalized as so: True, False just switch that and it will work. and there is no loop in the program...

Offline

#12 2011-06-20 04:06:12

duke11235
Member
Registered: 2009-10-09
Posts: 221

Re: [SOLVED]Python3 If/Else Loop (Noob Question)

Fixed. Thanks

What does this line do?

if questions(): pass

I changed it to if statements, but I'm curious as to its use. Is it just a placholder there?

Last edited by duke11235 (2011-06-20 04:08:45)

Offline

#13 2011-06-20 20:04:31

whitie
Member
Registered: 2011-03-13
Posts: 23

Re: [SOLVED]Python3 If/Else Loop (Noob Question)

duke11235 wrote:

What does this line do?

if questions(): pass

If questions() evaluates to True, nothing is to do.

A little bit more pythonic way:

#!/usr/bin/env python

from collections import namedtuple


Question = namedtuple('Question', 'question answer')

Q = (
    Question('What... is Your Name?\n',
             lambda x: x.replace('.', '').isdigit()),
    Question('What... is Your Favorite Color?\n',
             lambda x: x.lower() == 'blue'),
    Question('What... is The Airspeed Velocity of an Unladen Swallow?\n',
             lambda x: x.lower() == 'african or european swallow?')
)


def intro():
    print('***********************************************************')
    print('*            Gorge of Eternal Peril Challenge             *')
    print('*                                                         *')
    print('***********************************************************')
    print('\n')


def questions():
    answer_1 = input(Q[0].question)
    if Q[0].answer(answer_1):
        return
    answer_2 = input(Q[1].question)
    if not Q[1].answer(answer_2):
        return
    answer_3 = input(Q[2].question)
    if Q[2].answer(answer_3):
        print("   I don't know that! AAAAAUUUUUUGGGHHHH...")
        return True


if __name__ == '__main__':
    intro()
    if questions() is None:
        print('AAAAAUUUUUUGGGHHHH You have been cast in to the Gorge of '
              'Eternal Peril!')

Whitie

Offline

#14 2011-06-22 01:04:23

markbabc
Member
Registered: 2010-11-06
Posts: 157

Re: [SOLVED]Python3 If/Else Loop (Noob Question)

You joking?? you just called lambda functions more pythonic????

although i did like the use of the namedtuple

Offline

Board footer

Powered by FluxBB