Python 入门日记(七)—— 用户输入和 while 循环

2020.07.13 Python 入门的 Day6

成就:用户输入和 while 循环

  • 函数 input() 可用于读入字符串,使用 input() 读入的数据都被视作字符串。
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: lbwnb
# lbwnb
prompt = "If you tell us who you are, we can personalize the messages you see."
prompt += "\nWhat is your first name?"
name = input(prompt)
# If you tell us who you are, we can personalize the messages you see.
# What is your first name?lbw
# >>name:" lbw
  • 如果要读入整数类型,需要读入字符串之后强制类型转换;要读入浮点数也同理。
  • 但如果格式不对,编辑器会报错。
age = input()
age = int(age)
# 读入整数
age = float(age)
# 强制转换成浮点数
  • while 循环格式如下:
counter_number = 1
Active = True

while Active:
    counter_number += 1
    if counter_number >= 100:
        Active = False
    if counter_number == 89:
        break
  • break、continue 的使用与 C 语言一样。
posted @ 2020-07-13 15:58  Tree。  阅读(112)  评论(0编辑  收藏  举报