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

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'))

Related

How to give input in any Data Types in Python? [duplicate]

This question already has answers here:
Can the input() function in Python dynamically detect the input's data type?
(3 answers)
Closed 2 years ago.
I have one doubt in the Python Input method.
When I am entering input, it is always considering as String in Python. How to get the Input value in many data types.
As Example:
If I enter Integer value as input then the Code supposed to take that as Integer.
Code:
a=str(input("Enter A Value \n"))
In the above code, it converts my input always as String. Because I used str there.
If I remove str from there and if I type some numbers in the input will it take as an integer?
Python 3.x will always consider input as string you have to type cast each field manually.
int(input('Enter Number'))
str(input('Enter String'))

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

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

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))

How to code Python to accept only integers? [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 8 years ago.
I'm working on a Python 3.4 coding assignment that needs an integer input from the user. I am having trouble figuring out how to make the program accept only integer values; for instance, if the user inputs a float (i.e. "9.5"), the program will output, "That's not an integer! Try again."
Simple, use raw_input to get the string input, then call .isdigit() on it to see if it is an int. If it is, cast it to an int, then check it is within the valid range. Stick it all in a while loop so it keeps being called until a valid number is input, and you're all set.

Stop input function in python [duplicate]

This question already has answers here:
raw_input without pressing enter
(11 answers)
Closed 8 years ago.
I would like to stop the user to type in too many characters in the input function so it just stops the input() when too many characters are put in but the characters previously typed in stay. In this case I wouln't want to check if this is after the user has pressed enter but I would like to interrupt the function. Is this somehow possible?
Might be possible with a loop and msvcrt. Here is an example:
while len(string) < 10:
#Whatever length you want instead of 10
string += msvcrt.getch()
I don't believe this is possible, but if you're concerned about a maximum length (for a database value or something similar) you can use slicing to address excessively long entries:
maxlen = 256
userval = input("Enter value (max 256):")[:maxlen]

Categories