所以程序应该取一个数字,然后应该用文本打印这个数字。
print("Enter your number")
Number = int(input())
def number_to_text_ones(ones):
if ones == 1:
print("one")
elif ones == 2:
print("two")
elif ones == 3:
print("three")
elif ones == 4:
print("four")
elif ones == 5:
print("five")
elif ones == 6:
print("six")
elif ones == 7:
print("seven")
elif ones == 8:
print("eight")
elif ones == 9:
print("nine")
def number_to_text_tens(tens):
if tens == 2:
print("twenty ")
elif tens == 3:
print("thirty ")
elif tens == 4:
print("fourry ")
elif tens == 5:
print("fifty ")
elif tens == 6:
print("sixty ")
elif tens == 7:
print("seventy ")
elif tens == 8:
print("eighty ")
elif tens == 9:
print("ninety ")
def number_to_text_hundreds(hundreds):
if hundreds == 1:
print("one hundred")
elif hundreds == 2:
print("two hundreds")
elif hundreds == 3:
print("three hundreds")
elif hundreds == 4:
print("four hundreds")
elif hundreds == 5:
print("five hundreds")
elif hundreds == 6:
print("six hundreds")
elif hundreds == 7:
print("seven hundreds")
elif hundreds == 8:
print("eight hundreds")
elif hundreds == 9:
print("nine hundreds")
if Number == 0:
print("zero")
elif Number == 10:
print("ten")
elif Number == 11:
print("eleven")
elif Number == 12:
print("twelve")
elif Number == 13:
print("thirteen")
elif Number == 14:
print("fourteen")
elif Number == 15:
print("fifteen")
elif Number == 16:
print("sixteen")
elif Number == 17:
print("seventeen")
elif Number == 18:
print("eighteen")
elif Number == 19:
print("nineteen")
if Number < 10:
Result = Number % 10
number_to_text_ones(Result)
elif (Number >= 20 and Number < 100):
First = Number // 10
Second = (Number - (First * 10)) % 10
Result = First * 10 + Second
number_to_text_tens(First)
number_to_text_ones(Second)
elif (Number >= 100 and Number < 1000):
First = Number // 100
Second = (Number - (First * 100)) // 10
Third = (Number - (First * 100) - (Second * 10)) % 10
Result = First * 100 + Second * 10 + Third
number_to_text_hundreds(First)
number_to_text_tens(Second)
number_to_text_ones(Third)
问题是当我输入时,例如,895我的输出将是:
eight hundreds
ninety
five
我有几个问题:
如何让我的输出看起来更好?
有没有办法摆脱那些if语句并使用更高效的东西?像switch / case
答案 0 :(得分:1)
以下是答案:
print
函数有一个可选参数end
,它指定将在您打印的字符串末尾写入的内容。默认为新行('\n'
)。你想用
print('something', end=' ')
比第一次构建一个名称为整数的字符串更好,最后只使用一次打印。
解决这个问题的好方法是使用字典,例如:
name = {1: "one hundred",
2: "two hundreds",
3: "three hundreds"}[hundreds]
答案 1 :(得分:0)
你应该使用字典,因为它会减少代码行(if-else条件不再只是一个return语句就足够了)并且会让你的代码更好看,执行时间也会减少因为使用if-否则条件程序检查每个指定的条件,所以词典将帮助你很多
要将输出打印在一行中,这个更好
print(number_to_text_hundreds(First) + number_to_text_tens(Second) + number_to_text_ones(Third))
number_to_text_hundereds,number_to_text_tens和number_to_text_ones返回与传递给函数的值对应的值
答案 2 :(得分:0)
我建议你使用字符串列表如下:
print("Enter your number")
Number = int(input())
units = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
teens = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
tens = ['', '', "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
h = Number // 100
t = (Number % 100) // 10
u = (Number % 10)
result = []
if h == 0:
pass
elif h == 1:
result.append('one hundred')
else:
result.append(units[h] + ' hundreds')
if (t == 0):
if (u != 0):
result.append(units[u])
elif (h == 0):
result = ['zero']
elif (t == 1):
result.append(teens[u])
else:
result.append(tens[t])
if (u != 0):
result.append(units[u])
result = ' '.join(result)
print(result)
# input = 895 : result = 'eight hundreds ninety five'
# input = 507 : result = 'five hundreds seven'
# input = 120 : result = 'one hundred twenty'
# input = 071 : result = 'seventy one'
# input = 16 : result = 'sixteen'
# input = 0 : result = 'zero'