PDA

View Full Version : The python int() function doesn't work with raw_input()



l951b951
July 25th, 2009, 01:30 AM
I'm using the "Think Python" text to try to learn Python on my own. I ran into a question yesterday and asked on this forum. I've run into a question tonight. I read through "Read before posting" post, but I also have a question: Is this the right forum to ask basic beginner programming questions, or should I ask in a different forum (such as absolute beginner talk)?


My original question that I came here to ask is the following:

I'm having trouble understanding how to take user input in using raw_input() and converting that input into an integer (to use for comparison using >). The exercise I'm doing
“If any of the three lengths is greater than the sum of the other two, then you cannot
form a triangle. Otherwise, you can .”
1. Write a function named is_triangle that takes three integers as arguments, and that prints
either “Yes” or “No,” depending on whether you can or cannot form a triangle from sticks
with the given lengths.


The code I'm using is
def is_triangle(a,b,c):
if c>a+b or b>a+c or a>b+c:
print 'No'
else:
print 'Yes'

def user_input():
print 'Enter first value'
user_a=raw_input()
int(user_a)
print 'Enter second value'
user_b=raw_input()
int(user_b)
print 'Enter third value'
user_c=raw_input()
int(user_c)
is_triangle(int(user_a),int(user_b),int(user_c))


is_triangle(3,4,5)
user_input()

In the last line of user_input, I only get correct returns if I encapsulate int(user_a), etc. If I try to use user_a by itself, the returns come back incorrect because is_triangle takes them as strings, concatenates them and so the less than comparison comes back wrong. Also, "is_triangle(3,4,5)" is at the end as a comparison, I'm using those figures as my input.

Why can't I use int(user_a) immediately after I create user_a from raw_input()?

type(user_a) comes back as a string.

Can+~
July 25th, 2009, 01:36 AM
int(variable_name) doesn't change the variable in place, you should do


print 'Enter first value'
user_a = raw_input()
user_a = int(user_a)

Or immediately make it an int:


print 'Enter first value'
user_a = int(raw_input())

Also, you don't need that previous print, both raw_input() and input() accept an argument as question:


user_a = int(raw_input('Enter first value:'))

Rewriting the user_input() function as


def user_input():
user_a = int(raw_input("Enter the first value:"))
user_b = int(raw_input("Enter the second value:"))
user_c = int(raw_input("Enter the third value:"))

is_triangle(user_a, user_b, user_c)

l951b951
July 25th, 2009, 01:41 AM
Ah, ok.

Thanks both for the answer and the tip. I'm learning stuff slowly but surely.

l951b951
July 26th, 2009, 05:59 AM
Ok, here's a question that was buried in my original question. Is this the correct forum for someone who's learning python? I read the stickies, but none said outright whether it's ok or not to ask beginner questions in this forum, or is there a better place to ask?

thornmastr
July 27th, 2009, 01:03 AM
Ok, here's a question that was buried in my original question. Is this the correct forum for someone who's learning python? I read the stickies, but none said outright whether it's ok or not to ask beginner questions in this forum, or is there a better place to ask?

This is a forum about programming. Notice there is no set specifications as to language or skill level. You posted to the correct forum.

Robert