欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python从入门到实践重点整理及习题

程序员文章站 2024-03-26 11:50:11
...

Python从入门到实践重点整理及习题

#@用户输入与while循环
#@while循环

prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
message = ""
while message != 'quit':
    message = input(prompt)
    if message != 'quit':
        print(message)

#@使用标志
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
action = True
while action:
	message = input(prompt)
	if message == 'quit':
		action = False
	else:
		print(message)

#@使用break退出循环"
prompt = "\nPlease enter the name of a city you have visited"
prompt += "\n(Enter 'quit' when you are finished.) "

while True:
    city = input(prompt)
    if city == 'quit':
        break
    else:
        print("I'd love to go to " + city.title() + "!")
#@在循环中使用continue
current_number = 0
while current_number < 10:
    current_number += 1
    if current_number % 2 == 0:
       continue																														
    print(current_number)

'''  
@@@ 
有家电影院根据观众的年龄收取不同的票价:不到3岁的观众免费;3~12岁的观众为10美元;超过12岁的观众为15美元。请编写一个循环,在其中询问用
户的年龄,并指出其票价。
在while 循环中使用条件测试来结束循环。
使用变量active 来控制循环结束的时机。
使用break 语句在用户输入'quit' 时退出循环。
@@@
'''
active = True
while active:
    print("Enter 'quit' when you are finished.")
    age = input("请输入你的年龄:")
    if age == 'quit':
        active = False
    elif int(age) < 3:
        print("The price of you is free.")
    elif 3 <= int(age) <12:
        print("The price of you is $10.")
    elif 12 <= int(age) :
        print("The price of you is $15.")

相关标签: Python