python-用户输入和while循环

函数input()

比较大小要同类型:

  • age=iput()
  • 21
  • age=int(age)
  • age>=10
  • true
1 prompt = "If you tell us who you are, we can personalize the messages you see."
2 prompt += "\nWhat is your first name? "
3 #运算符+=在存储prompt中的字符串末尾附加一个字符串。其实可以这样理解:prompt + = '\nzifuchuang'+
4 name = input(prompt)
5 print("\nHello, " + name + "!")

求模运算符:两个数相除并返回余数。

1 number = input("Enter a number, and I'll tell you if it's even or odd: ")
2 print(type(number))  #输入为str
3 number = int(number)
4 print(type(number))  #转换为int
5 if number % 2 == 0:
6     print("\nThe number " + str(number) + " is even.")
7 else:
8     print("\nThe number " + str(number) + " is odd.")

while循环

while 判断条件:

  执行条件

在循环中使用continue:执行余下的代码并推出整个循环,执行continue语句,让python忽略余下的代码,并返回到循环的开头。

猜你喜欢

转载自www.cnblogs.com/BBS2013/p/12757458.html
今日推荐