New Posts  All Forums:Forum Nav:

Python help

post #1 of 21
Thread Starter 
I was bored so I began writing this random Python program, and have now hit a stop.


How do you get this to work without python telling me that it cannot add an int and a string?
Code:
variableOne = input(": ")
if variableOne > "25":
    print ("Too High!")

variableTwo = variableOne + 25



Note the problem on line 2, it's comparing two strings (raises errors if 25 is not in quotes), and later adding that string to a integer.
Any workarounds for this?
Red Thunder
(19 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k ASRock Fatal1ty Z68 Pro3 Gen3 Diamond Multimedia HD 5970 G.Skill Ripjaws X 8GB 
Hard DriveHard DriveOptical DriveCooling
OCZ Solid 3 60GB Seagate Barracuda 7200.9 160GB Lite-On iHAS424 Sunbeam Twister 120 
OSMonitorKeyboardPower
Windows 7 Ultimate 64-bit Samsung SyncMaster E1920X Logitech K120 Corsair GS700 
CaseMouseMouse PadAudio
NZXT Phantom Razer Naga None Realtek ALC892 
OtherOtherOther
Behringer Xenyx 502 Lepai LP-2020A+ Dayton Audio B652 
  hide details  
Reply
Red Thunder
(19 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k ASRock Fatal1ty Z68 Pro3 Gen3 Diamond Multimedia HD 5970 G.Skill Ripjaws X 8GB 
Hard DriveHard DriveOptical DriveCooling
OCZ Solid 3 60GB Seagate Barracuda 7200.9 160GB Lite-On iHAS424 Sunbeam Twister 120 
OSMonitorKeyboardPower
Windows 7 Ultimate 64-bit Samsung SyncMaster E1920X Logitech K120 Corsair GS700 
CaseMouseMouse PadAudio
NZXT Phantom Razer Naga None Realtek ALC892 
OtherOtherOther
Behringer Xenyx 502 Lepai LP-2020A+ Dayton Audio B652 
  hide details  
Reply
post #2 of 21
since nobody has helped you out yet, I will smile.gif

variableOne is a string (you read it in from command line so python picks string to hold that information in (also why the "25" needed the quotes, and most likely isn't doing quite what you expect!)

so...



if you made it this:
Code:
variableOne = input(": ")
variableTemp = int(variableOne)
if variableTemp > 25:
    print ("Too High!")

variableTwo = variableTemp + 25


it should work for you as expected (assuming the code block doesn't muck all the spacing up of course smile.gif


edit: fixed my freeking typo biggrin.gif
First WC, epic
(10 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 3930K RAMPAGE IV EXTREME NVIDIA GeForce GTX 670 NVIDIA GeForce GTX 670 
RAMRAMRAMRAM
Corsair  Corsair  Corsair  Corsair  
RAMHard Drive
Corsair  Crucial M4 
  hide details  
Reply
First WC, epic
(10 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 3930K RAMPAGE IV EXTREME NVIDIA GeForce GTX 670 NVIDIA GeForce GTX 670 
RAMRAMRAMRAM
Corsair  Corsair  Corsair  Corsair  
RAMHard Drive
Corsair  Crucial M4 
  hide details  
Reply
post #3 of 21

variableOne isn't a String. The input() function in Python takes input in the form of an int. CrashZero is probably confusing the input() function with the raw_input() function in Python.

Also, you don't need to keep 25 in the quotation marks since you're comparing to ints. You cannot compare an int with a String ("25").

 

Just remove the quotes around 25.

 

variableOne = input(": ")
if variableOne > 25:
    print ("Too High!")

variableTwo = variableOne + 25
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
post #4 of 21
Thread Starter 
Quote:
Originally Posted by CrashZero View Post

since nobody has helped you out yet, I will smile.gif

variableOne is a string (you read it in from command line so python picks string to hold that information in (also why the "25" needed the quotes, and most likely isn't doing quite what you expect!)

so...



if you made it this:
Code:
variableOne = input(": ")
variableTemp = int(variableOne)
if variableTemp > 25:
    print ("Too High!")

variableTwo = variableTemp + 25


it should work for you as expected (assuming the code block doesn't muck all the spacing up of course smile.gif


edit: fixed my freeking typo biggrin.gif
THANK YOU

I had found out about the int() function but had no idea how to use it
You're a saint biggrin.gif
Red Thunder
(19 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k ASRock Fatal1ty Z68 Pro3 Gen3 Diamond Multimedia HD 5970 G.Skill Ripjaws X 8GB 
Hard DriveHard DriveOptical DriveCooling
OCZ Solid 3 60GB Seagate Barracuda 7200.9 160GB Lite-On iHAS424 Sunbeam Twister 120 
OSMonitorKeyboardPower
Windows 7 Ultimate 64-bit Samsung SyncMaster E1920X Logitech K120 Corsair GS700 
CaseMouseMouse PadAudio
NZXT Phantom Razer Naga None Realtek ALC892 
OtherOtherOther
Behringer Xenyx 502 Lepai LP-2020A+ Dayton Audio B652 
  hide details  
Reply
Red Thunder
(19 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k ASRock Fatal1ty Z68 Pro3 Gen3 Diamond Multimedia HD 5970 G.Skill Ripjaws X 8GB 
Hard DriveHard DriveOptical DriveCooling
OCZ Solid 3 60GB Seagate Barracuda 7200.9 160GB Lite-On iHAS424 Sunbeam Twister 120 
OSMonitorKeyboardPower
Windows 7 Ultimate 64-bit Samsung SyncMaster E1920X Logitech K120 Corsair GS700 
CaseMouseMouse PadAudio
NZXT Phantom Razer Naga None Realtek ALC892 
OtherOtherOther
Behringer Xenyx 502 Lepai LP-2020A+ Dayton Audio B652 
  hide details  
Reply
post #5 of 21
Thread Starter 
Quote:
Originally Posted by {Unregistered} View Post

variableOne isn't a String. The input() function in Python takes input in the form of an int. CrashZero is probably confusing the input() function with the raw_input() function in Python.
Also, you don't need to keep 25 in the quotation marks since you're comparing to ints. You cannot compare an int with a String ("25").

Just remove the quotes around 25.
Code:
[code]variableOne = input(": ")
if variableOne > 25:
    print ("Too High!")

variableTwo = variableOne + 25
[/code]
That only raised errors for me -.-
Red Thunder
(19 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k ASRock Fatal1ty Z68 Pro3 Gen3 Diamond Multimedia HD 5970 G.Skill Ripjaws X 8GB 
Hard DriveHard DriveOptical DriveCooling
OCZ Solid 3 60GB Seagate Barracuda 7200.9 160GB Lite-On iHAS424 Sunbeam Twister 120 
OSMonitorKeyboardPower
Windows 7 Ultimate 64-bit Samsung SyncMaster E1920X Logitech K120 Corsair GS700 
CaseMouseMouse PadAudio
NZXT Phantom Razer Naga None Realtek ALC892 
OtherOtherOther
Behringer Xenyx 502 Lepai LP-2020A+ Dayton Audio B652 
  hide details  
Reply
Red Thunder
(19 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k ASRock Fatal1ty Z68 Pro3 Gen3 Diamond Multimedia HD 5970 G.Skill Ripjaws X 8GB 
Hard DriveHard DriveOptical DriveCooling
OCZ Solid 3 60GB Seagate Barracuda 7200.9 160GB Lite-On iHAS424 Sunbeam Twister 120 
OSMonitorKeyboardPower
Windows 7 Ultimate 64-bit Samsung SyncMaster E1920X Logitech K120 Corsair GS700 
CaseMouseMouse PadAudio
NZXT Phantom Razer Naga None Realtek ALC892 
OtherOtherOther
Behringer Xenyx 502 Lepai LP-2020A+ Dayton Audio B652 
  hide details  
Reply
post #6 of 21
no worries! I'm just glad I finally saw a programming releated question thread, with a question I could answer BEFORE {Unregistered} did (first time its happened since I joined OCN...LOL biggrin.gif
First WC, epic
(10 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 3930K RAMPAGE IV EXTREME NVIDIA GeForce GTX 670 NVIDIA GeForce GTX 670 
RAMRAMRAMRAM
Corsair  Corsair  Corsair  Corsair  
RAMHard Drive
Corsair  Crucial M4 
  hide details  
Reply
First WC, epic
(10 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 3930K RAMPAGE IV EXTREME NVIDIA GeForce GTX 670 NVIDIA GeForce GTX 670 
RAMRAMRAMRAM
Corsair  Corsair  Corsair  Corsair  
RAMHard Drive
Corsair  Crucial M4 
  hide details  
Reply
post #7 of 21
Thread Starter 
Quote:
Originally Posted by CrashZero View Post

no worries! I'm just glad I finally saw a programming releated question thread, with a question I could answer BEFORE {Unregistered} did (first time its happened since I joined OCN...LOL biggrin.gif
lol
Red Thunder
(19 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k ASRock Fatal1ty Z68 Pro3 Gen3 Diamond Multimedia HD 5970 G.Skill Ripjaws X 8GB 
Hard DriveHard DriveOptical DriveCooling
OCZ Solid 3 60GB Seagate Barracuda 7200.9 160GB Lite-On iHAS424 Sunbeam Twister 120 
OSMonitorKeyboardPower
Windows 7 Ultimate 64-bit Samsung SyncMaster E1920X Logitech K120 Corsair GS700 
CaseMouseMouse PadAudio
NZXT Phantom Razer Naga None Realtek ALC892 
OtherOtherOther
Behringer Xenyx 502 Lepai LP-2020A+ Dayton Audio B652 
  hide details  
Reply
Red Thunder
(19 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k ASRock Fatal1ty Z68 Pro3 Gen3 Diamond Multimedia HD 5970 G.Skill Ripjaws X 8GB 
Hard DriveHard DriveOptical DriveCooling
OCZ Solid 3 60GB Seagate Barracuda 7200.9 160GB Lite-On iHAS424 Sunbeam Twister 120 
OSMonitorKeyboardPower
Windows 7 Ultimate 64-bit Samsung SyncMaster E1920X Logitech K120 Corsair GS700 
CaseMouseMouse PadAudio
NZXT Phantom Razer Naga None Realtek ALC892 
OtherOtherOther
Behringer Xenyx 502 Lepai LP-2020A+ Dayton Audio B652 
  hide details  
Reply
post #8 of 21
Quote:
Originally Posted by White Fire View Post

That only raised errors for me -.-

what version of python are you running?

(i just ran it and it worked as expected...)
Edited by CrashZero - 8/31/12 at 9:11am
First WC, epic
(10 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 3930K RAMPAGE IV EXTREME NVIDIA GeForce GTX 670 NVIDIA GeForce GTX 670 
RAMRAMRAMRAM
Corsair  Corsair  Corsair  Corsair  
RAMHard Drive
Corsair  Crucial M4 
  hide details  
Reply
First WC, epic
(10 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 3930K RAMPAGE IV EXTREME NVIDIA GeForce GTX 670 NVIDIA GeForce GTX 670 
RAMRAMRAMRAM
Corsair  Corsair  Corsair  Corsair  
RAMHard Drive
Corsair  Crucial M4 
  hide details  
Reply
post #9 of 21
Quote:
Originally Posted by White Fire View Post

I was bored so I began writing this random Python program, and have now hit a stop.
How do you get this to work without python telling me that it cannot add an int and a string?
Code:
variableOne = input(": ")
if variableOne > "25":
    print ("Too High!")
variableTwo = variableOne + 25
Note the problem on line 2, it's comparing two strings (raises errors if 25 is not in quotes), and later adding that string to a integer.
Any workarounds for this?

Type casting. Cast the string to an int ( ideally, you should modify your code to catch the exception .. since you can never trust your user's input lol )
mod 1:
Code:
variableOne = input(": ")
if variableOne > "25":
    print ("Too High!")
variableTwo = int(variableOne) + 25

You generally can't numerically add different datatypes. VariableOne is a string while 25 is well... a number lol. The above would give vairbaleTwo as an integer type.

The problem is that if variableOne isn't an int, it would throw as exception on the
Code:
variableTwo = int(variableOne) + 25

A good solution for that would be to add some exception handling tongue.gif

Edit:

Wait. you used input() and not raw_input(). iirc, input tries to cast things to the correct type so variableOne should be an int ... what the hell is happening there? lol. the if statement is what needs to change. Ignore everything else I said tongue.gif
Edited by dklimitless - 8/31/12 at 9:08am
The Dark Knight
(13 items)
 
Rebirth
(13 items)
 
 
CPUMotherboardGraphicsRAM
i5 2500K. 4.5Ghz (Summer Clocks) ASUS P8P67 Pro ( Rev 3.0 ) MSI 6970 Lightning (downclocked) CFX with MSI 6... 8GB G.Skill Sniper 1600Mhz 
Hard DriveOptical DriveOSMonitor
1 TB Spinpoint F3 +2TB F4(backups) Random LG drive Win7 SP1 64bit ASUS VH236H @1080p 
KeyboardPowerCase
Saitek Gaming keyboard ... >_> Corsair TX750 Rosewill Gear x3 
CPUMotherboardGraphicsRAM
C2D P8600 @ 2.4Ghz (undervolted) Crappy dell Studio XPS one 4670M (1GB) I don't remember... 
Hard DriveOptical DriveOSMonitor
Crucial M4 4x Blu-ray ROM Win7 SP1 64bit 15.6" WLED @ 1080p 
KeyboardPower
Old Saitek Gaming Keyboard 90W adapter 
  hide details  
Reply
The Dark Knight
(13 items)
 
Rebirth
(13 items)
 
 
CPUMotherboardGraphicsRAM
i5 2500K. 4.5Ghz (Summer Clocks) ASUS P8P67 Pro ( Rev 3.0 ) MSI 6970 Lightning (downclocked) CFX with MSI 6... 8GB G.Skill Sniper 1600Mhz 
Hard DriveOptical DriveOSMonitor
1 TB Spinpoint F3 +2TB F4(backups) Random LG drive Win7 SP1 64bit ASUS VH236H @1080p 
KeyboardPowerCase
Saitek Gaming keyboard ... >_> Corsair TX750 Rosewill Gear x3 
CPUMotherboardGraphicsRAM
C2D P8600 @ 2.4Ghz (undervolted) Crappy dell Studio XPS one 4670M (1GB) I don't remember... 
Hard DriveOptical DriveOSMonitor
Crucial M4 4x Blu-ray ROM Win7 SP1 64bit 15.6" WLED @ 1080p 
KeyboardPower
Old Saitek Gaming Keyboard 90W adapter 
  hide details  
Reply
post #10 of 21
Quote:
Originally Posted by White Fire View Post

That only raised errors for me -.-

How? Just remove the quotes around "25" in your original code and it should work absolutely fine. I didn't change anything else from your code.
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming