Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

I have a problem with input.

Ok here is the code:

print ("Hello! \nWelcome to Ohm's law calculator")

print ("If you want to calculate voltage type 'V'\nIf you want to calculate current type 'I'\nIf you want to calculate resistance type 'R'")

while True:

my_input = input(": ")

if my_input == "V":

    num1 = float(input("Enter current: "))

    num2 = float(input("Enter resistance: "))

    result = num1 * num2

    print "Voltage is: " + result

    break

Sooo, how does input work, i saw this on internet, and i wanteed to make Ohm's law calculator, I only made for voltage and nothing happen when i run the code and type V. Please help, how can i make this work?? And i am working in python 2.7.13, thank you!

To make everyone's life easier, look at the markdown cheatsheet so it's easier to read your code

first three lines do not want to go to code, sorry for that i think now its better!

2 Answers

Python 2 and Python 3 are pretty different.

Kenneth is using Python 3.

The simplest answer is to use the raw_input function instead of the input.

This is because Python 2 will try to actually evaluate whatever you type in to the input function when you run the program.

For example if you had a variable called name in your program and you type "name" without the double quotes into the input when you run, it will actually fetch the data out of the name variable. Python 3's input automatically converts your input into a string and doesn't actually execute what you type.

To do the same thing Python 3's input does in Python 2, you can use the raw_input function.

The other ways to fix this, and probably the best ways, is to either install Python 3 on your machine (check out this Python course if you are on a Mac or this if you are on Windows) or Create a new workspace and code in workspaces.

A couple differences between Python 2 and Python 3:

  • Python 2 doesn't require parentheses for the print function while Python 3 does.
  • Python 2's input function is different than Python 3's input function (as said).
  • Python 2's for loop I think is a little different.
  • And much more...

I hope this helps. ~Alex

Thank you!