How to implement a simple user interaction program in Python (example)

不言
Release: 2018-09-21 16:38:03
Original
3954 people have browsed it

The content of this article is about how to implement simple user interaction programs (examples) in Python. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The programs we often use all have programs that interact with users, such as user interaction functions such as logging in to a web page and requiring you to enter your account number and password.

Let’s write a simple user input and output program code:

user1 = input("账号:")  #申明变量user1 储存获取用户输入的账号
password1 = input("密码:") #申明变量 password1 储存获取用户输入的账号
print(user1 , password1) #输出用户输入的账号密码
Copy after login

In this way, the user can be asked to enter the account password, and then output the obtained account password.

Let’s give another example:

Question: We have 100 yuan, we bought name1 and spent consumption yuan, how much money is left?

money1 = 100 #定义变量money1
name1 = input("名称:" ) #定义变量买了什么东西name1
consumption1 = input("价格:") #定义变量consumption1价格是多少
print(name1) #输出买的东西名称
print("找回客户", money1-int(consumption1), "块") #计算剩余多少钱,逗号是将各个部分的拼接起来,是独立的部分,所以互相不影响。
Copy after login

Or you can

print("Retrieve customer" str(money1-int(consumption1)) "block") #Calculate how much money is left, connect with plus sign, they are a whole, Therefore, each part must be converted into a character string form before it can be added and output.

This way you can output what you bought and how much money you got back. As shown below:

Here we should pay attention to some things:

int is the abbreviation of integer, which means integer

str is the abbreviation of string. It is a string

input All the data obtained is of string type, but the amount in money1 is an integer, so when we calculate the remainder, that is, money1-int(consumption1) here, we need to put the variable The string received by consumption1 is converted into an integer, using int. We use the first output method, separated by commas, which is equivalent to splicing between strings. The second output method uses the method to connect, so you need to convert the integer money1-int(consumption1) into a string, using str, so that it can be output.

The above is the detailed content of How to implement a simple user interaction program in Python (example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!