简体   繁体   English

一个简短的 python 程序

[英]A short python program

I am new to learning python and need to write two lines of code to get the result using comparison operators and not if blocks.我是学习 python 的新手,需要编写两行代码来使用比较运算符而不是 if 块来获得结果。 Help would be greatly appreciated, thanks.帮助将不胜感激,谢谢。

Using one of the comparison operators in Python, write a simple two-line program that takes the parameter n as input, which is an integer, and prints False if n is less than 100, and True if n is greater than or equal to 100.使用 Python 中的比较运算符之一,编写一个简单的两行程序,将参数 n 作为输入,即 integer,如果 n 小于 100,则打印 False,如果 n 大于或等于 100,则打印 True .

n = int(input())
print(n >= 100)

So it will print True if n is greater than or equal to 100 , and will print False if not, which in this case is less than 100 .因此,如果n大于或等于100 ,它将打印True ,否则将打印False ,在这种情况下小于100 Easier to read:更容易阅读:

n = int(input())
if n >= 100:
  print(True)
else:
  print(False)

or或者

n = int(input())
print(True if n>=100 else False)

As comparison operators return boolean value(True or False), this is explained in the value-comparisons section in the official documentation.由于比较运算符返回 boolean 值(True 或 False),这在官方文档的value-comparisons部分中进行了解释。

Taking what said before you should just print the comparison that will be True , first taking input and casting into an int :采取之前所说的,您应该只打印将是True的比较,首先接受input并转换为int

>>> number = int(input("Choose a number: "))
Choose a number: 100
>>> print(number >= 100)
True

the represented code will be 2 lines exclusing the input and the output printed:表示的代码将是 2 行,不包括输入和 output 打印:

number = int(input("Choose a number: "))
print(number >= 100)

This solution might be more readable:此解决方案可能更具可读性:

number = int(input("Choose a number: "))
print(True if number >= 100 else False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM