LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-23-2014, 08:10 PM   #1
hunterjoz1996
Member
 
Registered: Dec 2013
Location: Virginia Beach, VA
Distribution: Arch Linux
Posts: 59

Rep: Reputation: 6
Odd Python Error


Hi all. I am coding a little bit of software that takes your name and pay. If the pay is greater than 200, then the application will say you're cool. Then, it will mumtiply the pay by fifty. In this program, I put in the pay of 212. Instead of giving me 10600, I get a loop of 212. Does anyoone know why/
Begin code:
name = raw_input("Who are you?")
pay = raw_input("How much money do you get?")
int(pay)
if pay > 200:
print "Your %s, and your pay is %s! Your cool!" % (name, pay)
print pay*50, "This is what you would have if you multiplied your pay by fifty."
 
Old 04-23-2014, 08:20 PM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
When posting Python code indentation is essential. Indentation will only be preserved if you use code-tags to post your code. If you need information about code-tags have a look at my signature.
 
Old 04-23-2014, 08:35 PM   #3
hunterjoz1996
Member
 
Registered: Dec 2013
Location: Virginia Beach, VA
Distribution: Arch Linux
Posts: 59

Original Poster
Rep: Reputation: 6
Thanks for the resource. I got the if statement to work, the problem is the odd looping of 212. I don't understand why that happened, I guess python thought pay*50 meant loop it fifty times.
 
Old 04-23-2014, 09:50 PM   #4
Myk267
Member
 
Registered: Apr 2012
Location: California
Posts: 422
Blog Entries: 16

Rep: Reputation: Disabled
Quote:
Originally Posted by hunterjoz1996 View Post
Thanks for the resource. I got the if statement to work, the problem is the odd looping of 212. I don't understand why that happened, I guess python thought pay*50 meant loop it fifty times.
According to Python, pay holds a string, so it's going to return the result of multiplying the string instead of the int type: "212" * 50 = "212212212...". You can try out something a little simpler with strings using the addition operator: "212" + "212" = '212212'.

If you want pay to be an int, you need to either reset the value of pay or use another variable to hold the new value. Here's my edit that resets the value of pay to an int type:
Code:
name = raw_input("Who are you?")
pay = raw_input("How much money do you get?")
pay = int(pay)
if pay > 200:
    print "Your %s, and your pay is %s! Your cool!" % (name, pay)
print "%s: This is what you would have if you multiplied your pay by fifty." % (pay * 50)
You might want to check out the type() function, which will return the type of a python object, just in case you don't know, like this:
Code:
>>> my_money = raw_input("How much money? ")
How much money? 20
>>> type(my_money)
<type 'str'>
>>>
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Error: python-libs conflicts with python LDouglasLJr Linux - Software 2 10-05-2011 02:38 PM
Python 2.6 dependencies error installing python-sqlite2-2.5.5-1.1.i586.rpm jmp007 Linux - Newbie 1 02-11-2011 11:05 AM
[SOLVED] Python Installed; bash command: python base.py. gets syntax error in base.py cre84j Linux - Software 4 01-23-2011 04:46 PM
odd error with make, and its a source i have compiled many times with no error bensington butters Programming 1 07-01-2008 10:19 AM
odd error jetpig Linux - Newbie 0 01-14-2006 03:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration