Python input用户输入

函数input()的工作原理

  • 函数input()让程序暂停运行,等待用户输入一些文本,获取用户输入后,python将其变量存储在一个变量中,以方便实用。
  • input()接收一个参数,即向用户显示的提示或说明。
# 案例1
message = input("Tell me something,and I will repaeat it back to you : ")
print(message)
Tell me something,and I will repaeat it back to you : hello
hello

编写清晰的程序

  • 使用input()函数时,都应指向清晰而易于明白的提示。
  • 可将提示存储在一个变量中,再将变量传递给函数input()
name = input("Please enter your name: ")
print("Hello, " + name + "!")
Please enter your name: yegeli
Hello, yegeli!
prompt = "If you tell us who you are, we can personalize the message you see. "
prompt += "\nWhat is your first name? "
name = input(prompt)
print("\nHello, " + name + "!")
If you tell us who you are, we can personalize the message you see. 
What is your first name? yegeli

Hello, yegeli!

使用int()来获取数值输入

  • 用户输入的数值为字符串表示
  • 函数int()将数字的字符串表示转换为数值表示
  • 将数值输入用于计算和比较前,务必将其转换为数值表示
age = input("How old are you? ")
age
How old are you? 21





'21'
age = input("How old are you ? ")
age = int(age)
age >=18
How old are you ? 21





True
height = input("How tall are you ,in inches? ")
height = int(height)

if height >= 36:
    print("\nYou're tall enough to ride!")
else:
    print("\nYou'll be able to ride when you're a little order.")
How tall are you ,in inches? 30

You'll be able to ride when you're a little order.

求模运算符

  • % 它将两个数想出并返回余数:
  • 一个数可被另一个数整除,余数就为 0 ,因此求模运算符将返回 0 。你可利用这一点来判断一个数是奇数还是偶数
# 奇数偶数判断
number = input("Enter a number, and I'll tell you if it's enven or odd: ")
number = int(number)

if number % 2 == 0:
    print("\nThe number " + str(number) + " is even")
else:
    print("\nThe number " + str(number) + " is odd")
Enter a number, and I'll tell you if it's enven or odd: 22

The number 22 is even

练习

# 1.汽车租赁
# 编写一个程序,询问用户要租赁什么样的汽车,并打印一条消息,如 “Let me see if I can find you a Subaru”
prompt = "brother,DO you want to rent a car? "
message = input(prompt)
print("Let me see if I can fin you a " + message.title() + " !")
brother,DO you want to rent a car? subaru
Let me see if I can fin you a Subaru !
# 2.餐馆订位
# 编写一个程序,询问用户有多少人用餐。如果超过 8 人,就打印一条消息,指出没有空桌;否则指出有空桌。
order_food = input("How many people order meals? ")
order_food = int(order_food)
if order_food >= 8:
    print("Sorry there are no vacant tables.")
else:
    print("\nGreat,there are still tables available.")
How many people order meals? 4

Great,there are still tables available.
# 3.10的整数倍
# 让用户输入一个数字,并指出这个数字是否是 10 的整数倍
multiple = input("Please enter a number! ")
multiple = int(multiple)
if multiple % 10 == 0:
    print(str(multiple) + " is 10 multiple.")
else:
    print(str(multiple) + " is not 10 multiple.")
Please enter a number! 110
110 is 10 multiple.
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YEGE学AI算法

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值