I am struggling with my python temperature unit converter [duplicate] - python

This question already has answers here:
Getting user input [duplicate]
(5 answers)
Closed 5 years ago.
For a challenge I am tasked with creating a unit converter that can change the units. I chose degrees Celsius to Fahrenheit. I am quite new to Python. My problem is that I ask a question on the code e.g.
print("Enter Value: ")
How do I make it so that the value that a user enters becomes the variable f for Fahrenheit which can then be changed to Celsius so I can do this..
print((f - 32) / 1.8)
Can anyone help and explain it in a way a beginner can understand?

Assuming you're using Python3, what you need is:
temp=input("Temperature please?")
print((int(temp)-32)/1.8)
Also, please look up the docs Jacek linked to so that you understand what's really going on here.

Use input() function:
Input and Output Docs

temp = 0
# while loop
# wait until user set a input
while not temp:
# default type in input is "str"
user_input = input("Enter Value: ")
if user_input.isdigit():
temp = user_input
# we know every char is digit
print (((int(temp)-32)/1.8))

Related

How to read input from user's keyboard [duplicate]

This question already has answers here:
How to read keyboard input?
(5 answers)
Closed 11 months ago.
I'm a noob in programming (in python), I want to know how I could read data from user's keyboard in order to make a calculator for example based on the numbers he entered.
Thank you.
This is for sure a duplicate question but you can use this:
a = int(input("Enter the first number"))
b = int(input("Enter the second number"))
... do what you want with a and b.
The input() statement is used for this purpose.
But the input statement accepts string input, so in order to take integral input, you need to convert the input to integers. Code:
a = int(input('Enter a Number'))
This will prompt the user to provide input and then store it in the variable a. You can then use it anywhere. In the same way, if you want to input decimal numbers, you need to convert the type to float:
a = float(input('Enter a Number'))

Python: Help me, what am i doing wrong? [duplicate]

This question already has answers here:
String comparison in Python: is vs. == [duplicate]
(4 answers)
Closed 2 years ago.
I'm learning how to code and to start i want crate a command that when i write "Dio" for example it writes me "Error 404: dio non esiste" but it says that the code is wrong, what am i doing wrong? here's the code
name = int(input("Come ti chiami? "))
if name is "Antonio":
print("Eh no")
if name is "Dio":
print("Error 404: Dio non esiste")
if name is "Dio porco":
print("lol")
You have taken input of type int:
Instead just take :
name = input("Come ti chiami? ")
which would be string by default,
and use "==" to compare.
But if you have just started learning, I would suggest first go through the python documentation which would give you a basic understanding of how to write python codes.
is checks if two values are exactly the same reference. Since you're inputting one value from a user and the other is hard coded, they won't be the same reference. Instead, you should use the == operator. Additionally, if you're inputting a name, you shouldn't convert the input to an int:
name = input("Come ti chiami? ")
if name == "Antonio":
print("Eh no")
if name == "Dio":
print("Error 404: Dio non esiste")
if name == "Dio porco":
print("lol")

How can I make my program create variables automatically and print them by it self? [duplicate]

This question already has answers here:
How can you dynamically create variables? [duplicate]
(8 answers)
Closed 3 years ago.
I've got an idea for a program that I'm not sure how to implement. How can I make the program ask the user something -- input() -- as many times as user responds until x command is written and (and this is the hard part) create variables automatically (e.g answer1 = x, answer2 = x, answer3 = x, answer4 = x) with their respective value assigned?
And how could I then print all those variables without having to write them manually, for example, if at the beginning 6 variables were created then the 6 variables will be printed.
I'm not asking you to write it all for me, conversely I want you to give me some tips, some functions or ideas and I will be more than grateful. Thanks in advance!!
If I understand your problem correctly, python lists should be a better solution to your problem. You can simply create an empty list:
answers = []
and then follow with something like this:
string = input()
while string != x:
answers.append(string)
string = input()
print(answers)

How to re-prompt user input after incorrect type of data is given in Python 3.6 [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 5 years ago.
This question has not been asked in relation to Python 3.6 .
I need a solution to re-prompt a specific user input question in a series of input questions if data is given as anything else but an integer or float in this case.
Lets say they input the correct float data for the first question, but then lets say that input a string character into the second question. That outputs a "ValueError: could not convert string to float: ".
Is there a way using looping or another method to re-prompt the SECOND input question that they failed to put integer/float data in? Furthermore, can you re-prompt only the second question instead of having to start over and re-prompt to the FIRST question?
counter = 0
counter += float(input("What is number 1?"))
counter += float(input("What is number 2?"))
counter += float(input("What is number 3?"))
print(counter)
EDIT: I did read the posted articles containing 9 answers which is similar, but non of them dealt with multiple input questions one after another. The provided answered were helpful, but I still don't quite get how to re-prompt the 2nd or 3rd question after an incorrect data type is entered. In summary: I would like the program to re-prompt the question that failed rather than having the user have to begin at question 1.
Something like this pseudo-code would work:
for q in questions:
while True:
ask_question
if question_result_validated:
break

How can I make my python code accept keyboard input? [duplicate]

This question already has answers here:
How do I read from stdin?
(25 answers)
Closed 17 days ago.
For example, if the program asks the user to press enter on their keyboard to continue, the program can recognise it & continue. I'm still a student so not really sure, is it possible..?
I think you are looking for input command
You need to use the input statement and assign the input to variables like this:
name = input("What's your name? ")
print("Hello there, " + name + "!")
It's that simple!
Use the input function as well as if statements:
x = input("What's 1+1?")
if x == 2:
print("correct")
else:
print("Wrong")

Categories