7 用户输入和while循环

# 函数input(),获取字符串输入

#message = input("Tell me:")

#print(message)

#-------------------------------------------------------

#一种创建多行字符串的方式

# ~ prompt = 'Hello everyone.'

# ~ prompt += '\nYour name is :'

# ~ name = input(prompt)

# ~ print('Hello,'+name.title())

# ~ 使用函数input()时,Python将用户输入解读为字符串。

#-------------------------------------------------------

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

# ~ height = input('How tall are you,in inches?')

# ~ height = int(height) #将变量height转换为数值

# ~ if height >=36:

    # ~ print('\nYou are too old!')

# ~ else:

    # ~ print('\nYou are so young!')

#------------------------------------------------------- 


# 求模运算符,返回余数

# ~ number = input("Input a number,you can see it's even or odd")

# ~ number = int(number)

# ~ if number % 2 == 0:

    # ~ print('\nThe number is even')

# ~ else:

    # ~ print('\nThe number is odd')

#------------------------------------------------------- 

# while循环

# while循环都必须有停止运行的途径

# ~ current_number = 1

# ~ while current_number <= 5:

    # ~ print(current_number)

    # ~ current_number += 1 

    # ~ current_number = current_number + 1

#------------------------------------------------------- 

# ~ prompt = '\nTell me something,and I will repeat it.'

# ~ prompt += "\nEnter 'quit',its over."

# ~ message = ''

# ~ while message != 'quit':

    # ~ message = input(prompt)

    # ~ if message != 'quit':

        # ~ print(message)

#------------------------------------------------------- 

#使用标志

# ~ prompt = '\nTell me something,and I will repeat it.'

# ~ prompt += "\nEnter 'quit',its over."

# ~ active = True

# ~ while active:

    # ~ message = input(prompt)

    # ~ if message == 'quit':

        # ~ active =False

    # ~ else:

        # ~ print(message)

#-------------------------------------------------------

# break 退出循环

# 在任何Python循环中都可使用break语句。

# ~ prompt = '\nPlease enter the name of a city you have visited:'

# ~ prompt += "\nEnter 'quit' when you finished"

# ~ while True:

    # ~ city = input(prompt+'\n')

    # ~ if city == 'quit':

        # ~ break

    # ~ else:

        # ~ print("I'd love to go to "+city.title()+"!")

#-------------------------------------------------------

# continue 回到循环开始

# ~ number = 0

# ~ while number < 10:

    # ~ number += 1

    # ~ if number % 2 ==0:

        # ~ continue

    # ~ print(number)

#-------------------------------------------------------

# 在列表之间移动元素

# ~ unconfirmed_users = ['dave','harry','alice']

# ~ confirmed_users = []

# ~ while unconfirmed_users:

    # ~ current_user = unconfirmed_users.pop()

    # ~ #函数pop()移除列表中的一个元素(默认最后一个元素),并且返回该元素的值

    # ~ print("Verifying user: "+current_user.title())

    # ~ confirmed_users.append(current_user)

# ~ for confirmed_user in confirmed_users:

    # ~ print(confirmed_user.title())

# ~ print(unconfirmed_users)

#-------------------------------------------------------

#删除包含特定值的所有列表元素

# ~ pets = ['cat','dog','cat','goldfish','rabbit','cat','dog']

# ~ print(pets)

# ~ while 'cat' in pets:

    # ~ pets.remove('cat')

# ~ print(pets)

#-------------------------------------------------------

# 使用用户输入来填充字典

# ~ responses = {}

# ~ active = True

# ~ while active == True:

    # ~ name = input("\nWhat's your name?")

    # ~ response = input('Which food do you like to eat?')


    # ~ responses [name] = response  # 将答卷存储在字典中


    # ~ repeat = input("\nAny one else?(y/n)")

    # ~ if repeat =='n':

        # ~ active = False

    # ~ print('\nResult\n')

    # ~ for name,response in responses.items():

        # ~ print(name.title()+' like to eat '+response+'.')


#-------------------------------------------------------

#作业1

# ~ sandwich_orders = [

    # ~ 'Black Forest Ham',  #黑森林火腿三明治

    # ~ 'Chicken & Bacon Ranch Melt',  #鸡肉培根三明治

    # ~ 'Cold Cut Combo', #冷餐三明治

    # ~ 'Classic Tuna',  #经典金枪鱼三明治

    # ~ 'Turkey Breast',#火鸡胸肉三明治

    # ~ ]

# ~ finished_sanwiches = []

# ~ while sandwich_orders:  #for循环和while循环的用法区别

    # ~ sandwich = sandwich_orders.pop()

    # ~ print('I made your '+sandwich)

    # ~ finished_sanwiches.append(sandwich) #列表添加元素

# ~ print(finished_sanwiches)

# ~ print(sandwich_orders)


#作业2

# ~ sandwich_orders = [

    # ~ 'Black Forest Ham',  #黑森林火腿三明治

    # ~ 'Chicken & Bacon Ranch Melt',  #鸡肉培根三明治

    # ~ 'pastrami' #五香烟熏牛肉

    # ~ 'Cold Cut Combo', #冷餐三明治

    # ~ 'Classic Tuna',  #经典金枪鱼三明治

    # ~ 'pastrami', #五香烟熏牛肉

    # ~ 'Turkey Breast',#火鸡胸肉三明治

    # ~ 'pastrami', #五香烟熏牛肉

    # ~ ]

# ~ finished_sanwiches = []

# ~ print('\nOur pastrami is out of stock')

# ~ while sandwich_orders:

    # ~ sandwich = sandwich_orders.pop()

    # ~ if sandwich == 'pastrami':

        # ~ continue

    # ~ else:

        # ~ print('I made your '+sandwich)

        # ~ finished_sanwiches.append(sandwich)

# ~ print(finished_sanwiches)

# ~ print(sandwich_orders)

#作业3

places = []

active = True

while active:

    place = input('\nWhere would you go?')

    places.append(place)


    repeat = input('\nAnyone else?(y/n)')

    if repeat == 'n':

        break

print(places)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容