我正在制作一个可以转换数字存储单元的程序。就像,你可以问它有多少比特是x量的兆比特。我只是用一个if语句对它进行测试,但我无法打印答案。
import re
UNITS = ["bit", "byte", "exabit", "exabyte", "gigabit", "gigabyte",
"kilobit", "kilobyte", "megabit", "megabyte", "petabit", "petabyte",
"terabit", "terabyte"]
PLURAL_UNITS = ["bits", "bytes", "exabits", "exabytes", "gigabits",
"gigabytes", "kilobits", "kilobytes", "megabits", "megabytes", "petabits",
"petabytes", "terabits", "terabytes"]
PROMPT_1 = "How many "
PROMPT_2 = "are in "
def convert():
first_unit = raw_input(PROMPT_1)
second_unit = raw_input(PROMPT_2)
number_of = re.findall('\d+', second_unit)
number = number_of[0]
if first_unit == PLURAL_UNITS[0] and second_unit == " " + PLURAL_UNITS[1]:
answer = float(number)*8
print(answer)
convert()
答案 0 :(得分:0)
看看这一行:
if first_unit == PLURAL_UNITS[0] and second_unit == " " + PLURAL_UNITS[1]:
使用此行,只有在第一个输入为"bits"
且第二个输入为" bytes"
时,才会打印代码。输入任何其他内容会使if语句(包括您的print
)下的代码被跳过。
答案 1 :(得分:0)
它没有打印,因为它没有达到您的print
声明。您if
声明的一个或两个条件是False
。要检查哪个,打印他们的结果:
def convert():
first_unit = raw_input(PROMPT_1)
second_unit = raw_input(PROMPT_2)
number_of = re.findall('\d+', second_unit)
number = number_of[0]
print('first check:', first_unit == PLURAL_UNITS[0])
print('second check:', second_unit == " " + PLURAL_UNITS[1])
if first_unit == PLURAL_UNITS[0] and second_unit == " " + PLURAL_UNITS[1]:
answer = float(number)*8
print(answer)
convert()
答案 2 :(得分:-2)
在python2中,print是一个语句,而不是一个函数。 打印答案