Python problem

Need help with C, C++, perl, python, etc?

Python problem

Postby 0bm » 2007-03-28 10:48

I am VERY new to programming. I'm trying to make a simple script that will collect two numbers from the user, tell the user which is greater, and then ask them if they want to do it all again. (yes, I am aware of the uselessness of this program. I'm just using it as a learning tool)

The problem is: I keep getting stuck in an infinite loop. Even if the user enters 'N' to quit, the program keeps running.

here's the code:
Code: Select all
loop_ini = True
input = True

def loop():
    while loop_ini:
        z = raw_input('Would you like to test another value[y/n]? ')
        if z == 'y':
            break
        if z == 'Y':
            break
        if z == 'n':
            input = False
        if z == 'N':
            input = False
        else:
            print 'invalid input'
            continue

def print_max(x, y):
    if x > y:
        print x, 'is maximum.'
    else :
        print y, 'is maximum.'


while input:
    a = int(raw_input('Enter first value: '))
    b = int(raw_input('Enter second value: '))
    if a == b:
        print 'Values are equal.'
        loop()
    else:
        print_max(a, b)
        loop()


Please help! :( :( :cry:
Image
User avatar
0bm
 
Posts: 10
Joined: 2007-03-25 09:17

Postby _FOCUS_ » 2007-03-28 11:04

I think second if must be elif

Code: Select all
elif z == 'N'
If you love something, let it go. If it comes back to you, it's yours. If it's run over by a car, you don't want it.
User avatar
_FOCUS_
 
Posts: 205
Joined: 2006-02-22 18:11

Postby Dargor » 2007-03-28 11:05

yay i figured it out, im new to this two, you need to import sys. then sys.exet()

like
Code: Select all
#!/usr/bin/python
import sys
loop_ini = True
input = True

def loop():
    while loop_ini:
        z = raw_input('Would you like to test another value[y/n]? ')
        if z == 'y':
            break
        if z == 'Y':
            break
        if z == 'n':
            sys.exit()
        if z == 'N':
            sys.exit()
        else:
            print 'invalid input'
            continue

def print_max(x, y):
    if x > y:
        print x, 'is maximum.'
    else :
        print y, 'is maximum.'


while input:
    a = int(raw_input('Enter first value: '))
    b = int(raw_input('Enter second value: '))
    if a == b:
        print 'Values are equal.'
        loop()
    else:
        print_max(a, b)
        loop()
User avatar
Dargor
 
Posts: 671
Joined: 2006-08-14 08:54
Location: New Zealand, Hamilton

Postby _FOCUS_ » 2007-03-28 11:16

This may be because of that you do not use any of gui. I think it will work with it. At this stage it is not essential codes.
If you love something, let it go. If it comes back to you, it's yours. If it's run over by a car, you don't want it.
User avatar
_FOCUS_
 
Posts: 205
Joined: 2006-02-22 18:11

Postby 0bm » 2007-03-29 02:37

You guys are awesome. Thanks :D
Image
User avatar
0bm
 
Posts: 10
Joined: 2007-03-25 09:17

Postby germain » 2007-04-01 20:53

Code: Select all
loop_ini = True
input = True

def loop():
    while loop_ini:
        z = raw_input('Would you like to test another value[y/n]? ')
        if z == 'y':
            break
        if z == 'Y':
            break
        if z == 'n':
            input = False
            loop_ini = False
        if z == 'N':
            input = False
            loop_ini = False
        else:
            print 'invalid input'
            continue

def print_max(x, y):
    if x > y:
        print x, 'is maximum.'
    else :
        print y, 'is maximum.'


while input:
    a = int(raw_input('Enter first value: '))
    b = int(raw_input('Enter second value: '))
    if a == b:
        print 'Values are equal.'
        loop()
    else:
        print_max(a, b)
        loop()


:wink:
User avatar
germain
 
Posts: 90
Joined: 2006-09-25 23:10
Location: Mexico. Ajuuuua!!!

...

Postby ccaruso » 2007-04-26 14:52

That seems pretty complicated. Wouldn't this meet your specified requirements(sorry about the old bump but I'm just learning Python too)?

Code: Select all
a=0
b=0
ans="y"
while ans == "y" or ans == "Y":
        a = input("Number 1: ")
        b = input("Number 2: ")
        if a > b:
                print "Number 1 is greater."
        else:
                print "Number 2 is greater."
        else:
                print "Numbers are equal."
        ans =raw_input("Continue? (y/n):")
ccaruso
 
Posts: 3
Joined: 2007-04-26 14:49

Re: ...

Postby Lavene » 2007-04-26 18:32

ccaruso wrote:That seems pretty complicated. Wouldn't this meet your specified requirements(sorry about the old bump but I'm just learning Python too)?

Code: Select all
a=0
b=0
ans="y"
while ans == "y" or ans == "Y":
        a = input("Number 1: ")
        b = input("Number 2: ")
        if a > b:
                print "Number 1 is greater."
        else:
                print "Number 2 is greater."
        else:
                print "Numbers are equal."
        ans =raw_input("Continue? (y/n):")

That will not work. The second 'else:' has no 'if'. You are on the right track though... use "elif" instead:
Code: Select all
a=0
b=0
ans="y"
while ans == "y" or ans == "Y":
        a = input("Number 1: ")
        b = input("Number 2: ")
        if a > b:
                print "Number 1 is greater."
        elif a < b:
                print "Number 2 is greater."
        elif a == b:
                print "Numbers are equal."
        ans =raw_input("Continue? (y/n):")

A very boring way to do it though ;)
And you could expand it a little by making sure it is a number that's entered, that it is an integer etc. (And you should really not use "input", it's a huge security risk so you might as well get used to using "raw_input"). It's actually not a bad exercise in my opinion.

Tina
Lavene
Site admin
 
Posts: 5096
Joined: 2006-01-04 04:26
Location: Oslo, Norway

...

Postby ccaruso » 2007-04-26 19:42

Shame on me, I added that second else into the reply when I realized I hadn't accounted for equality, and didn't actually test the code. Thanks, and why is input a security risk compared to raw_input? My understanding of it so far is "use input for numbers, raw_input for strings". :oops:

Edit- Just Googled it up, and you weren't kidding about it being a security issue. The first couple of hits I found were like this.
ccaruso
 
Posts: 3
Joined: 2007-04-26 14:49

Postby Lavene » 2007-04-26 19:58

Basically input() takes the input and try to evaluate it as python code. So a clever user can construct a an input that compromises the system. raw_input() returns a string.

You can try it in the interactive shell:

Code: Select all
>>> a = input("input:")
input:4+5
>>> a
9
>>> b = raw_input("Input:")
Input:4+5
>>> b
'4+5'


Try different inputs of python code and see what happens.
Some googleing should also find quite a bit about it. (Edit: Which I see you did) :)

Tina
Lavene
Site admin
 
Posts: 5096
Joined: 2006-01-04 04:26
Location: Oslo, Norway


Return to Programming

Who is online

Users browsing this forum: No registered users and 3 guests

fashionable