首页 > 编程语言 > 详细

python基础(六)

时间:2020-09-07 17:57:25      阅读:72      评论:0      收藏:0      [点我收藏+]
#函数input(),程序暂停运行,等待用户输入一些文本,获取输入后,将其存储在一个变量中
message = input("tell me")
print(message)
tell mehi
hi
name = input("Please enter your name:")
print("Hello, " + name + "!")
Please enter your name:cui
Hello, cui!
prompt = "hihi"
prompt = "\nwhat is your name"

name = input(prompt)
print("\nHello, " + name + "!")
what is your namecui

Hello, cui!
#使用int()来获取数值输入,将用户输入解读为字符串,无法与数值进行比较
age = input("How old are you?")
How old are you?22
age
‘22‘
age >= 18
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-4ce6028355cc> in <module>
----> 1 age >= 18

TypeError: ‘>=‘ not supported between instances of ‘str‘ and ‘int‘
age = int(age)
age >= 18
True
height = input("How tall are you, in inches")
height = int(height)
How tall are you, in inches71
if height >= 36:
    print("\nYou are tall enough to ride!")
else:
    print("\nYou‘ll be able to ride")
 
You are tall enough to ride!
#求模运算符
4%3
#用于判断奇数偶数
number = input("enter a number, and I‘ll tell you if it‘s even or odd:")
number = int(number)

if number % 2 == 0:
    print("\nThe number " + str(number) + "is even.")
else:
    print("\nThe number " + str(number) + "is odd.")
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)
Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.hihi
hihi

Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.露露
露露

Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.xixi
xixi

Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.quit
#用于判断奇数偶数
number = input("enter a number, and I‘ll tell you if it‘s even 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 even or odd:51

The number 51is odd.
#while循环
current_number = 1
while current_number <= 5:
    print(current_number)
    current_number += 1
1
2
3
4
5
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)
    print(message)
Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.hi
hi

Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.hello
hello

Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.quit
quit
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)
Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.hi

Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.hello

Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.quit
#使用标志
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter ‘quit‘ to end the program."

active = True
while active:
    message = input(prompt)
    
    if message == quit:
        active = False
    else:
        print(message)
Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.hi
hi

Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.hello
hello

Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.quit
#使用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("mmmmmm" + city.title() + "!")
Please enter the name of a city you have visited.
(enter ‘quit‘ when you are finished.)nanjing
mmmmmmNanjing!

Please enter the name of a city you have visited.
(enter ‘quit‘ when you are finished.)guangdong
mmmmmmGuangdong!

Please enter the name of a city you have visited.
(enter ‘quit‘ when you are finished.)quit
Please enter the name of a city you have visited.
(enter quit when you are finished.)nanjing
mmmmmmNanjing!

Please enter the name of a city you have visited.
(enter quit when you are finished.)guangdong
mmmmmmGuangdong!

Please enter the name of a city you have visited.
(enter quit when you are finished.)quit
#continue
number = 0
while number < 10:
    number += 1
    if number % 2 == 0:
        continue
    print(number)
1
3
5
7
9
#避免无限循环
#处理列表和字典
unconfirmed_users = [baba,mama,jing]
confirmed_users = []

while unconfirmed_users:
    current_user = unconfirmed_users.pop()
    print("vertify:" + current_user.title())
    confirmed_users.append(current_user)
vertify:Jing
vertify:Mama
vertify:Baba
print("new:")
for confirmed_user in confirmed_users:
    print(confirmed_user.title())
new:
Jing
Mama
Baba
#删除特定值的所有列表元素
pet = [dog,cat,dog,goldfish,cat,rabbit,cat]
print(pet)
[‘dog‘, ‘cat‘, ‘dog‘, ‘goldfish‘, ‘cat‘, ‘rabbit‘, ‘cat‘]
pets = [dog,cat,dog,goldfish,cat,rabbit,cat]
while cat in pets:
    pets.remove(cat)
    
print(pets)
[dog, dog, goldfish, rabbit]
#使用用户输入来填充字典
responses = {}

#设置一个标志,指出调查是否继续
polling_active = True
while polling_active:
    #提示输入被调查者的名字和回答
    name = input("\nWhat is your name?")
    response = input("Which mountain would you like to climb someday?")
    
    #将答卷存储在字典中
    responses[name] = response
    
    #看看是否还有人要参与调查
    repeat = input("Would you like to let another person respond?(yes/no)")
    if repeat == no:
        polling_active = False
What is your name?cui
Which mountain would you like to climb someday?huang
Would you like to let another person respond?(yes/no)yes

What is your name?lu
Which mountain would you like to climb someday?zijin
Would you like to let another person respond?(yes/no)yes

What is your name?ting
Which mountain would you like to climb someday?tai
Would you like to let another person respond?(yes/no)no
 #调查结束,显示结果
print("\n--- Poll Results ---")
for name,response in responses.items():
    print(name + " would like to climb " + response + ".")
--- Poll Results ---
cui would like to climb huang.
lu would like to climb zijin.
ting would like to climb tai.

python基础(六)

原文:https://www.cnblogs.com/Cookie-Jing/p/13627980.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!