輸入字符串類型 int
Create a program that asks the user to enter their name and age.Print out a message to them,Tell them a year they will be full 100 歲.注意:對於此練習,You are expected to write the year explicitly(So next year is out of date).If you want to do it in a generic way,請參見練習 39.
例外:
"\n
與按 Enter 按鈕相同)要獲取Python 3 User input in ,the command you used input()
.Store the result in a variable,and use it to your heart's content.請記住,Even if they enter numbers,The result you get from the user will also be a string.
例如:
name = input("Give me your name: ")print("Your name is " + name)
This will be in the terminal(or shell,you are running Python 中)What is printed in is:
>>> name = input("Give me your name: ")Give me your name: 宇宙之一粟>>> print("Your name is " + name)Your name is 宇宙之一粟>>>
input()
What happens at the end is,It waits for the user to type something and press Enter .Only when the user presses ENTER Execute the program to continue after executing the program.
你從 input()
What you get in the function is a string.你能做什麼?
首先:Turn a string into a number.Suppose you are inputting a number to the user 100% 肯定的.You can turn strings into functions int()
的整數:
age = input("Enter your age: ")age = int(age)
在這兩種情況下,Age will have an integer variable,Now you can do math with it.
(請注意,您還可以使用 str()
The function does the exact opposite of turning integers into strings)
第二:Do math with strings.
print("Were" + "wolf")print("Door" + "man")print("4" + "chan")print(str(4) + "chan")
Multiplication works the same way:
print(4 * "test")
But division and subtraction don't work that way.就乘法而言,The idea of multiplying two strings together is not well defined.What does it mean to multiply two strings first?但是,It makes sense to specify somehow that the string is multiplied by a number - Just repeat the string as many times as you want.
Try all the arithmetic operations with numbers and strings in your own program - The best way to learn is to try it out!