python入门 --- input + if/else


# if [condition]:  # condition is ture, then run code1 and code2
#     code1...
#     code2...  # four spaces indentation. can have only one code line, or more than one code lines
# else:  # condition is false
#     code3...
# code...  # this code line is not a part of the if/else structure
# -----
# if [condition]:
#     code...
# elif [condition]:
#     code...
# elif [condition]:
#     code...
# else:
#     code...

# input will get str type
weight = input("pls input your weight(kg):")
height = input("pls input your height(m):")
bmi = float(weight) / (float(height) ** 2)
print("your BMI is " + str(bmi))
if bmi <= 18.5:
    print("shou")
elif bmi <= 25:
    print("normal")
elif bmi <= 30:
    print("pang")
else:
    print("pang+")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Index The Python Tutorial 1 1. Whetting Your Appetite 4 2. Using the Python Interpreter 6 2.1. Invoking the Interpreter 7 2.1.1. Argument Passing 8 2.1.2. Interactive Mode 8 2.2. The Interpreter and Its Environment 9 2.2.1. Source Code Encoding 9 3. An Informal Introduction to Python 10 3.1. Using Python as a Calculator 11 3.1.1. Numbers 11 3.1.2. Strings 13 3.1.3. Lists 19 3.2. First Steps Towards Programming 21 4. More Control Flow Tools 24 4.1. if Statements 24 4.2. for Statements 24 4.3. The range() Function 25 4.4. break and continue Statements, and else Clauses on Loops 27 4.5. pass Statements 28 4.6. Defining Functions 29 4.7. More on Defining Functions 31 4.7.1. Default Argument Values 31 4.7.2. Keyword Arguments 33 4.7.3. Arbitrary Argument Lists 35 4.7.4. Unpacking Argument Lists 36 4.7.5. Lambda Expressions 37 4.7.6. Documentation Strings 38 4.7.7. Function Annotations 39 4.8. Intermezzo: Coding Style 39 5. Data Structures 40 5.1. More on Lists 41 5.1.1. Using Lists as Stacks 43 5.1.2. Using Lists as Queues 43 5.1.3. List Comprehensions 44 5.1.4. Nested List Comprehensions 46 5.2. The del statement 47 5.3. Tuples and Sequences 48 5.4. Sets 50 5.5. Dictionaries 51 5.6. Looping Techniques 53 5.7. More on Conditions 55 5.8. Comparing Sequences and Other Types 56 6. Modules 57 6.1. More on Modules 58 6.1.1. Executing modules as scripts 60 6.1.2. The Module Search Path 61 6.1.3. “Compiled” Python files 61 6.2. Standard Modules 62 6.3. The dir() Function 63 6.4. Packages 66 6.4.1. Importing * From a Package 68 6.4.2. Intra-package References 69 6.4.3. Packages in Multiple Directories 70 7. Input and Output 70 7.1. Fancier Output Formatting 70 7.1.1. Old string formatting 75 7.2. Reading and Writing Files 76 7.2.1. Methods of File Objects 77 7.2.2. Saving structured data with json 79 8. Errors and Exceptions 81 8.1. Syntax Errors 81 8.2. Exceptions 81 8.3. Handling Exceptions 82 8.4. Raising Exceptions 86 8.5. User-defined Exceptions 87 8.6. Defining Clean-up Actions 88 8.7. Predefined Clean-up Actions 89 9. Classes 90 9.1. A Word About Names and Objects 91 9.2. Python Scopes and Namespaces 91 9.2.1. Scopes and Namespaces Example 94 9.3. A First Look at Classes 95 9.3.1. Class Definition Syntax 95 9.3.2. Class Objects 96 9.3.3. Instance Objects 97 9.3.4. Method Objects 98 9.3.5. Class and Instance Variables 99 9.4. Random Remarks 100 9.5. Inheritance 102 9.5.1. Multiple Inheritance 104 9.6. Private Variables 105 9.7. Odds and Ends 106 9.8. Iterators 106 9.9. Generators 108 9.10. Generator Expressions 109 10. Brief Tour of the Standard Library 110 10.1. Operating System Interface 110 10.2. File Wildcards 111 10.3. Command Line Arguments 111 10.4. Error Output Redirection and Program Termination 112 10.5. String Pattern Matching 112 10.6. Mathematics 113 10.7. Internet Access 114 10.8. Dates and Times 114 10.9. Data Compression 115 10.10. Performance Measurement 115 10.11. Quality Control 116 10.12. Batteries Included 117 11. Brief Tour of the Standard Library — Part II 117 11.1. Output Formatting 118 11.2. Templating 119 11.3. Working with Binary Data Record Layouts 121 11.4. Multi-threading 121 11.5. Logging 122 11.6. Weak References 123 11.7. Tools for Working with Lists 124 11.8. Decimal Floating Point Arithmetic 126 12. Virtual Environments and Packages 127 12.1. Introduction 127 12.2. Creating Virtual Environments 128 12.3. Managing Packages with pip 129 13. What Now? 131 14. Interactive Input Editing and History Substitution 133 14.1. Tab Completion and History Editing 133 14.2. Alternatives to the Interactive Interpreter 133 15. Floating Point Arithmetic: Issues and Limitations 134 15.1. Representation Error 139 16. Appendix 141 16.1. Interactive Mode 141 16.1.1. Error Handling 141 16.1.2. Executable Python Scripts 142 16.1.3. The Interactive Startup File 142 16.1.4. The Customization Modules 143
### 回答1: Python是一门简洁、易读且功能强大的编程语言,非常适合初学者入门。下面是一个来自《Python编程入门》的编程题目以供参考。 题目:编写一个Python程序,输出从1到100的所有偶数。 解答如下: ```python for num in range(1, 101): if num % 2 == 0: print(num) ``` 以上程序使用了for循环来遍历从1到100的数字。在每次遍历时,使用if语句判断当前数字是否是偶数,如果是,则输出该数字。代码中的%运算符用于判断一个数是否能够被2整除,如果结果为0,则说明该数是偶数。 运行以上代码,即可输出从1到100之间的所有偶数。 在Python中,编写一个简单的程序只需要几行代码就能完成。这也是为什么Python是非常适合初学者入门的原因之一。不仅如此,Python还有丰富的库和模块,可以用于各种用途,无论是数据分析、网络编程还是网站开发。希望这个简单的编程题目能够帮助你入门Python编程。 ### 回答2: 以下是一个Python入门编程题目的答案,用300字中文回答: 题目要求编写一个程序,实现将一个正整数的每一位数字相加并输出结果。 假设输入的正整数为n,首先将n转换为字符串,然后遍历字符串中的每一位数字,将其转换为整数并累加到一个变量sum中。 最后输出sum的值作为结果。 考虑到一些边界情况,例如输入为0或者负数,可以在程序中添加一些判断,如果输入小于等于0,则输出错误提示信息。 具体的程序实现如下: ```python n = int(input("请输入一个正整数:")) if n <= 0: print("输入的数字不合法!") else: n_str = str(n) sum = 0 for digit in n_str: sum += int(digit) print("每一位数字相加的结果为:", sum) ``` 在程序中,首先通过input函数获取用户输入的数字n,并将其转换为整数。 然后,使用一个if-else语句判断输入的数字是否合法,如果不合法则输出错误提示信息。 对于合法的输入,将n转换为字符串n_str,并初始化sum变量为0。 接下来,使用一个for循环遍历n_str中的每一位数字,并将其转换为整数,累加到sum变量中。 最后,使用print函数输出每一位数字相加的结果。 这个程序是一个简单的Python入门编程题目,通过编写该程序,可以加深对于Python基础语法的理解和应用。 ### 回答3: 题目一:猜数字游戏 题目描述: 编写一个猜数字的小游戏程序。程序随机生成一个1到100之间的整数,用户通过输入猜测的数字,程序根据猜测的大小给出相应的提示,直到用户猜中为止。 解题思路: 1. 导入random模块,使用random.randint()函数生成一个1到100之间的随机整数作为答案。 2. 使用while循环,每次循环中提示用户输入一个猜测的数字,并将其转换为整数类型。 3. 判断用户输入的数字与答案的大小关系,给出相应的提示。如果猜中了,跳出循环;否则继续下一轮循环。 4. 提示用户猜测次数,并输出答案。 代码示例: import random answer = random.randint(1, 100) guess_times = 0 while True: guess = int(input("请输入一个1到100之间的整数:")) guess_times += 1 if guess > answer: print("猜大了,请再试一次。") elif guess < answer: print("猜小了,请再试一次。") else: print("恭喜你,猜对了!") break print("你一共猜了%d次,答案是%d。" % (guess_times, answer)) 题目二:字符串反转 题目描述: 编写一个函数,接收一个字符串作为参数,返回将该字符串反转后的结果。 解题思路: 1. 使用[::-1]切片操作实现字符串反转。 代码示例: def reverse_string(s): return s[::-1] # 测试示例 s = "Hello, World!" print(reverse_string(s)) # 输出:!dlroW ,olleH 题目三:计算素数 题目描述: 编写一个函数,接收一个正整数n作为参数,返回从1到n之间的所有素数。 解题思路: 1. 定义一个空列表prime_nums用于存放素数。 2. 使用for循环从2遍历到n,对于每个数判断是否为素数。 3. 判断素数的方法是,如果一个数能被2到其平方根范围内的任意一个数整除,即不为素数。 4. 如果一个数没有被整除,则加入prime_nums列表中。 代码示例: import math def prime_numbers(n): prime_nums = [] for num in range(2, n + 1): is_prime = True for i in range(2, int(math.sqrt(num)) + 1): if num % i == 0: is_prime = False break if is_prime: prime_nums.append(num) return prime_nums # 测试示例 n = 100 print(prime_numbers(n)) # 输出:[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] 以上是三个基础的Python编程题目的解答和代码示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值