python基础六——用户输入和while循环

包含有input函数的python文件需要在终端运行。

message = input("tell me somthing, I will repeat back to you:")
print(message)

input()函数进行字符输入。

prompt = "please input your name:"
prompt += "\nwhat is your first name!"
name = input(prompt)
print("hellow " + name + " !")

int()函数可以获取数值输入

height = input("How tall are you")
height = int(height)
if height >= 36:
    print("\nYou are tall enought to ride!")
else:
    print("\nYou can ride if you are a litter order!")

"%"是求余运算符号。

number = input("Enter a number:")
number = int(number)
if number %2 == 0 :
    print("\nThe number " + str(number) + " is even.")
elif number%2 == 1:
    print("\nThe number " + str(number) + " is odd.")

while循环举例:

current_number = 1
while current_number <= 5:
    print(current_number)
    current_number += 1

让用户选择什么时候推出。

prompt = "please input your name, I will repeat back to you:"
prompt += "\n Enter  'quit' to end the program. "
message = ""
while message != 'quit':
    message = input(prompt)
    if message != 'quit':
        print(message)

在列表之间移动数据

unconfirmed_users = ['alice','brian','candace']
confirmed_users = []
while unconfirmed_users:
    current_user = unconfirmed_users.pop() #每次从列表末尾删除未验证的客户
    print("Verified Users:" + current_user.title())
    confirmed_users.append(current_user)
print("following users have been confirmed:")
for confirmed_user in confirmed_users:
    print(confirmed_user.title())

删除列表中的特定元素

pets = ['dog','cat','goldfish','dog','cat','rabbit']
print(pets)
while 'cat' in pets:
    pets.remove('cat')
print(pets)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值