Python 3.6 Input output with variables - io

I have a question for python 3.6. I am trying to make an input-output system for a raspberry pi. I am fairly new to python and haven't found anything on this topic.
So far I have this:
j = input ("type a number")
n = input ("type a number")
j*n
The inputs work then the multiplication doesn't. Please help!

a = int(input ("type a number: "))
b = int(input ("type a number: "))
print ("a*b = ", a*b)
In the first example, input was not defined. It was just printing the input value. When input value was defined as the integer(int), it performed arithmetic operation and gave desired value.

Related

Beginner coder. Trying to convert string to integer in Python 3.X [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 months ago.
I am a at the very beginning of learning to code in python and am following a tutorial. I attempted to convert this string into an integer to get it to simply add 5 and 6. Here is what I have
No matter what I do, I get 5+6 = 56. Here is what I have:
first_num = (input('Please enter a number '))
second_num = (input('Please enter another number '))
print int((first_num) + int(second_num))
I tried using a comma instead of a plus sign, as a few places suggested. I also tried using int in front of the input line to convert the inputs themselves from strings to integer.
I expect it to add 5 + 6 = 11. I keep getting 56.
I'm not positive what version of Python I'm using, but I know I'm using VS Code and it is Python 3.X. i just don't know what the X is. Here is a screenshot
edit: I have resolved this question. I was not saving the file before I ran it. Therefore every time I tried to change something it was just running the saved, incorrect file. Thanks to those that tried to help me.
In Python when you add two strings together you concatenate the values.
For an easier example:
string_one = "hello"
string_two = " "
string_three = "there"
final = string_one + string_two + string_3
print(final) # hello there
To add them together mathematically, you need to ensure the values are ints, floats, decimals...
one = 1
two = 2
final = one + two
print(final, type(final)) # 3 int
So for your code:
first_num = int(input('Please enter a number'))
second_num = int(input('Please enter another number'))
final = first_num + second_num
print(final) # will give you the numbers added
However, you are casting to ints based on user input, so I would ensure you are also catching the errors that occur when a user enters a value that cannot be cast to an int, like hi. For example:
try:
first_num = int(input('Please enter a number'))
second_num = int(input('Please enter another number'))
print(first_num + second_num) # will give you the numbers added, if ints
except ValueError as ex:
# if any input val is not an int, this will hit
print("Error, not an int", ex)
Try this
first_num = int(input('Please enter a number '))
second_num = int(input('Please enter another number '))
sum=first_num+second_num
print (sum)

Beginner python - simple Pounds to Kilos

The instructions are very simple, but I am struggling none the less. We have learned very few things in this class so far, so I am looking for a very simplistic answer.
The instructions are as follows "Write a Python program that will prompt the user to enter a weight in pounds, then
convert it to kilograms and output the result. Note, one pound is .454 kilograms"
What i have so far is
print("Pounds to Kilos. Please Enter value in pounds.")
x=input('Pounds: ')
float(x)
print(x * .454)
You are converting the variable x's value to the float, but not assigning it to anything. So, the real value which x variable holds never changed. You did not edit the x variable in fact. You can try something like this;
print("Pounds to Kilos. Please Enter value in pounds.")
x=float(input('Pounds: '))
print(x * .454)
However, using functions in that nested manner is not recommended. Instead, initialize a new variable to hold the new float-converted value;
print("Pounds to Kilos. Please Enter value in pounds.")
x = input('Pounds: ')
x_float = float(x)
print(x_float * .454)
Going from your code example I would do something like this.
print("Pounds to Kilos. Please Enter value in pounds.")
x = float(input('Pounds: '))
print(x * 0.454, "kg")
EDIT
Maybe instead of having the calculation in the print() statement I add a separate variable for it, including the advise about float from the other solution.
print("Pounds to Kilos. Please Enter value in pounds.")
x = (input('Pounds: '))
kg = float(x) * 0.454
print(kg, "kg")
Explanation: First you ask the user's weight using input command, then you can print a message saying converting to kgs.. (it's optional). create a new variable called weight_kgs to convert the user's input to a float, then multiply the input by 0.45. after that convert the weight_kgs variable to a string once again by making a new variable called final_weight, so that you can join the input by user to a string. At the end print a message telling the user's weight. print("you weight is " + final_weight)
```
weight_lbs = input("enter your weight(lbs): ")
print("converting to kgs...")
weight_kgs = float(weight_lbs) * 0.45
final_weight = str(weight_kgs)
print("your weight is " + final_weight + "kgs") # line 5
```
I hope you got it.

User input with "SublimeRepl" for a python script

I am running a simple python script and using Sublime Text 3 as IDE using SublimeRepl for user input.
The below script always prints the else part even though I give a number less than 5.
num = input ("Enter the number bw 1-5")
if num in range(1,5):
print("Valid option")
else:
print("Invalid")
This has worked. Input is always a String, so we need to typecast to Int.
num = int(input ("Enter the number bw 1-5"))
if num in range(1,5):print("Valid option")
else:print("Invalid")

Python 3.5.1 Introduction to Python 2.1 Mark Clarkson - While Loop Issue

I'm working my way through this set of tutorials. In section 3.2a - While Loops the following code is supposed to loop until the user enters the target number (7) then display a congratulations message however regardless of what number is entered Python either gives a right answer or a wrong answer, even 7 will sometimes flag a wrong answer. I know there are other ways to perform this sort of task but I would like to get the code from the tutorial working.
targetNumber = 7
guess = input("Guess a number between 1 and 10 ")
while guess != targetNumber:
print("Wrong, try again ")
guess = input("Guess a number between 1 and 10 ")
print("Congratulations - that's right!")
You should convert the target numger to an string before comparison. Also, you should exclude the congratulations message from the loop. I would suggest :
targetNumber = str(7)
guess = input("Guess a number between 1 and 10 ")
while guess != targetNumber:
print("Wrong, try again ")
guess = input("Guess a number between 1 and 10 ")
print("Congratulations - that's right!")
The detail is that input returns a string and if you compare a string to an integer, it will always return false.
Python's input function (or raw_input in Python 2.x) returns a string entered by the user. targetNumber, on the other hand, is an integer. In the Python Interpreter, try:
>>> 7 == "7"
False
You need to cast the user's input to an integer first.
try:
guess = int(input("Please enter a number: "))
except ValueError:
print("That is not a valid number!")

When I enter a negative number it prints 'sorry' not the absolute number, what a I doing wrong?

def function_1():
var_1 = input('''Enter any number''')
if type(var_1) == int:
print (abs(var_1))
else:
print ('''sorry''')
function_1()
When I enter in a negative number it prints the else statement 'sorry' instead of giving me the absolute number
In Python 3, input() returns always a string.
In Python 2 input() evaluates the text as a Python expression, and raw_input() returns a string, this behavior is error prone as the most intuitive input mode can introduce different errors and unexpected behaviors. For that reason in Python 3 input() behaves like raw_input() does in Python 2.
You can convert the input to int with:
var_1 = int(input())

Resources