message = input("Tell me something, and I will repeat it back to you: ") print(message)
Tell me something, and I will repeat it back to you: 你好
你好
1 2 3 4
prompt = "If you tell us who you are, we can personalize the messages you see." prompt += "\nWhat is your first name? " name = input(prompt) print("\nHello, " + name + "!")
If you tell us who you are, we can personalize the messages you see.
What is your first name? KJ
Hello, KJ!
# 使用int()将input()的字符串转成整型 height = input("How tall are you, in inches? ") height = int(height) if height >= 36: print("\nYou're tall enough to ride!") else: print("\nYou'll be able to ride when you're a little older.")
How tall are you, in inches? 71
You're tall enough to ride!
求模运算符%是一个很有用的工具,它将两个数相除并返回余数
1 2 3 4 5 6 7
# 用于判断奇偶数 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: 12341241451
The number 12341241451 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) print(message)
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program. 画面上,天空是铁青色混合着火焰的颜色,唯一的一株巨树矗立着,已经枯死的树枝向着四面八方延伸,织成一张密网,支撑住皲裂(jūnliè)的天空。
画面上,天空是铁青色混合着火焰的颜色,唯一的一株巨树矗立着,已经枯死的树枝向着四面八方延伸,织成一张密网,支撑住皲裂(jūnliè)的天空。
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. 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. 我不再迷茫 思念是唯一的行囊
我不再迷茫 思念是唯一的行囊
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. quit
使用标志
可定义一个变量,用于判断整个程序是否处于活动状态。这个变量被称为标志
1 2 3 4 5 6 7 8 9 10 11 12
# 把这个标志命名为active(可给它指定任何名称) prompt = "\n我会重复你的话: " prompt += "\n输入‘quit’退出 " active = True while active: message = input(prompt) if message == 'quit': active = False else: print(message)
# 设置一个标志,指出调查是否继续 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 # 调查结束,显示结果 print("\n--- Poll Results ---") for name, response in responses.items(): print(name + " would like to climb " + response + ".")
What is your name? A
Which mountain would you like to climb someday? 黄山
Would you like to let another person respond? (yes/ no) yes
What is your name? B
Which mountain would you like to climb someday? 白云山
Would you like to let another person respond? (yes/ no) yes
What is your name? C
Which mountain would you like to climb someday? 岳麓山
Would you like to let another person respond? (yes/ no) no
--- Poll Results ---
A would like to climb 黄山.
B would like to climb 白云山.
C would like to climb 岳麓山.