Python-7.2-用户输入和while-练习

练习一:让用户输入一个数字,并指出这个数字是否是10的整数倍

prompt = "Enter a number : "
number = input(prompt)
number = int(number)
if number % 10 ==0 :
    print("The number " + str(number) + 
          " is integer multiples of 10.")
else :
    print("The number " + str(number) + 
          " is not integer multiples of 10.")

Enter a number : 100
The number 100 is integer multiples of 10.

练习二:编写一个循环,提示用户输入一系列的水果

  • 每当用户输入一种水果后,都打印一条信息
  • 在while循环中使用条件测试来结束循环
  • 使用变量active来控制循环结束的时机
  • 使用break语句用户输入’quit’时退出循环
prompt = "\nThere are all kinds of fruits."
prompt += "\nEnter your favorite fruit : "
active = "True"
while active :
    fruit = input(prompt)
    if fruit != 'quit' : 
        print("Favorite fruit : " + 
              str(fruit.title()))
    else :
        print("OVER")
        break

There are all kinds of fruits.
Enter your favorite fruit : apple
Favorite fruit : Apple

There are all kinds of fruits.
Enter your favorite fruit : banana
Favorite fruit : Banana

There are all kinds of fruits.
Enter your favorite fruit : quit
OVER

练习三:编写一个循环,在其中询问用户的年龄,并指出其票价

  • 电影院根据观众的年龄收取不同的票价:
    • 不到3岁的观众免费
    • 3-12岁观众为10
    • 超过12岁为15
prompt = "How old are you ? "
prompt = "Enter you age : "
avtive = "True"
while active :
    age = " "
    age = input(prompt)
    age = int(age)
    if age < 3 :
        print("You shoule pay for 0.")
    elif age < 12 :
        print("You should pay for 10.")
    else :
        print("You should pay for 15")

Enter you age : 2
You shoule pay for 0.
Enter you age : 6
You should pay for 10.
Enter you age : 88
You should pay for 15

练习四:将用户的列表中的字符串全部转换成小写,再存储它们

users = ["jack" , "Tony" ,"KEvin"]
new_users = [ ]
for user in users :
    print(user.lower())
    new_users.append(user.lower())
print(users)
print(new_users)

jack
tony
kevin
[‘jack’, ‘Tony’, ‘KEvin’]
[‘jack’, ‘tony’, ‘kevin’]

练习五:在列表之间移动元素

  • 创建一个名为fruits的列表,在其中包含三种水果的种类
  • 再创建一个名为bought_fruits的空列表
  • 遍历列表fruits,对于其中的每种水果,都打印一条消息,如My favorite fruit is apple.,并将每种水果移到列表bought_fruits
  • 所有的水果购买完成,打印一条消息,将这些水果列出来
#使用for循环
fruits = ['apple','banana','pear']
bought_fruits = [ ]
for fruit in fruits :
    print("My favorite fruit is " + fruit + ".")
    bought_fruits.append(fruit)
print("\nThere are the fruits I bought : ")
for bought_fruit in bought_fruits :
    print('\t' + bought_fruit.title())

My favorite fruit is apple.
My favorite fruit is banana.
My favorite fruit is pear.

There are the fruits I bought :
Apple
Banana
Pear

#使用while循环
fruits = ['apple','banana','pear']
bought_fruits = [ ]
while fruits :
    fruit = fruits.pop()
    print("My favorite fruit is " + fruit + ".")
    bought_fruits.append(fruit)
print("\nThere are the fruits I bought : ")
for bought_fruit in bought_fruits :
    print("\t" + bought_fruit.title())

My favorite fruit is pear.
My favorite fruit is banana.
My favorite fruit is apple.

There are the fruits I bought :
Pear
Banana
Apple

练习五:删除列表中所有的特定值

  • 顾客想要购买苹果,店铺里的苹果已售罄,使用remove()删除列表中的苹果
  • 打印出顾客能够买的水果
print("We do not have any apples.")
fruits = ['apple','banana','pear','apple','apple']
while 'apple' in fruits :
    fruits.remove('apple')
print("\nYou can buy another fruits : ")
for fruit in fruits :
    print("\t" + fruit.title())

We do not have any apples.

You can buy another fruits :
Banana
Pear

练习六:编写一个程序,调查用户想去的地方

  • 使用让用户输入的提示
  • 并编写一个打印调查结果的代码块
name_place = { }
active = True 
while active :
    names = input("\nEnter your name : ")
    places = input("Enter you favorite place : ")
    name_place[names] = places
    response = input("Would you like to let another person respond?(yes or no)")
    if response == 'no':
        active = False
        break
print("\n---Poll Results---")
print(name_place)
for name , place in name_place.items():
    print(str(name.title()) + " favorite place is " + str(place) + ".")

Enter your name : jack
Enter you favorite place : china
Would you like to let another person respond?(yes or no)yes

Enter your name : kevin
Enter you favorite place : america
Would you like to let another person respond?(yes or no)no

—Poll Results—
{‘jack’: ‘china’, ‘kevin’: ‘america’}
Jack favorite place is china.
Kevin favorite place is america.

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值