+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2019-03(1)

2019-05(1)

2019-06(1)

2019-07(7)

2019-08(118)

Python编程:从入门到实践第七章读书笔记7.1函数input()的工作原理

发布于2020-09-20 18:36     阅读(954)     评论(0)     点赞(19)     收藏(1)


0

1

2

3

4

5

6

7

8

9

Python编程:从入门到实践第七章读书笔记7.1函数input()的工作原理

#coding:gbk

#使用while循环让程序不断地运行,直到指定的条件不满足为止
#交互式程序

#7.1函数input()的工作原理

#parrot.py
message = input("Tell me something, and I will repeat it back to you: ")
print(message)

##7.1.1编写清晰的程序
#greeter.py
name = input("Please enter your name: ")
print("Hello, " + name + "!")

prompt = "If you tell us who you are, we can personalize the messages you see."    #有时,提示可能超过一行,如你需要指出输入的原因,在这时,可将提示存储在一个变量中,再将变量传递给函数input()
prompt += "\nWhat is your first name?"   #运算符+=在存储在prompt中的字符串末尾附加一个字符串

name = input(prompt)
print("Hello, " + name + "!")

##7.1.2使用int()来获取数值输入
age = input("How old are you? ")
print(age)

#>>> age = input("How old are you? ")
#How old are you? 21
#>>> age
#'21'
#>>> age = input("How old are you? ")
#How old are you? 21
#>>> age >= 18
#Traceback (most recent call last):
#  File "<stdin>", line 1, in <module>
#TypeError: '>=' not supported between instances of 'str' and 'int'
#>>> age = input("How old are you? ")
#How old are you? 21
#>>> age = int(age)
#>>> age >= 18
#True
#>>>

#rollercoaster.py
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 are a little older.")

#求模运算符
##处理数值信息时,求模运算符%是一个很有用的工具,它将两个数相处并返回余数

#4 % 3
#1
#>>> 5 % 3
#2
#>>> 6 % 3
#0
#>>> 7 % 3
#1

#求模运算符不会指出一个数是另一个数的多少倍,而只指出余数是多少
#可用来判短奇数和偶数

#even_or_odd.py
number = input("Enter a number, and I will 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.")

##7.1.4在python2.7中获取输入
#在python2.7中,应用raw_input()代替input()来提示用户输入



原文链接:https://blog.csdn.net/weixin_46667735/article/details/108635645

0

1

2

3

4

5

6

7

8



所属网站分类: 技术文章 > 博客

作者:不上班你养我呀

链接: https://www.pythonheidong.com/blog/article/535138/cdb726986284dc171029/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

19 0
收藏该文
已收藏

评论内容:(最多支持255个字符)