Two minutes to understand the input function in python
The input function is a built-in function in python, which reads from the standard input Read a string into a string and automatically ignore newlines. Let's take a look at the specific usage of the input function.
#Function input
message = input("Tell me something, and I will repeat it back to you: ") print(message) message1 = input() print(message1)
#How to create a multi-line string
prompt1="type someting" prompt2=prompt1+": "+input() print(prompt2)
prompt = "If you tell us who you are, we can personalize the messages you see." prompt += "\nWhat is your first name? " name = input(prompt) print("\nHello, " + name + "!")
#The default input input is characters, so type conversion should be performed when character comparison is required
age=input("How old are you?") age=int(age) if(age>=18): print("You have the right to vote!") else: print("Sorry,you have no right to vote!")
#Modulo operator
c1=input("Please input a number,and i will do modulo operation for it.") c1=int(c1) c2=input("Please input another number,and i will do modulo operation for it.") c2=int(c2) c3=c1%c2 print(c3)
# Car rental: Write a program that asks the user what kind of car he wants to rent and prints a message, such as "Let me see if I can find you a Subaru".
input("What kind of car would you like to hire?") print("Let me see if I can find you a Subaru")
#Restaurant reservation: Write a program to ask the user how many people are dining. If there are more than 8 people, print a message indicating that there are no empty tables; otherwise, indicate that there are empty tables.
p1=input("How many people in the dinner?") p1=int(p1) if(p1>8): print("Sorry,There is no empty table.") else: print("ok,There is an empty table")
#Integer multiples of 10: Let the user enter a number and indicate whether the number is an integer multiple of 10.
n1=input("Please input a number")n1=int(n1)if(n1%10==0):print(n1+" is an integer multiple of 10.")else: print(n1+" is not an integer multiple of 10.")
The input function is a built-in function in Python that reads a string from the standard input and automatically ignores newlines.
That is to say, all forms of input are processed as strings. If you want to get other types of data, perform forced type conversion. By default there is no
prompt string (prompt string). Given a prompt string, the prompt string will be output to the standard before reading the standard input. If the
end of file character (end of file) is encountered, an EOFError will be triggered.
Thank you everyone for reading, I hope you will benefit a lot.
This article is reproduced from: https://blog.csdn.net/qq_24135817/article/details/79818479
Recommended tutorial: "python tutorial"
The above is the detailed content of Learn about the input function in python in two minutes. For more information, please follow other related articles on the PHP Chinese website!