我目前正在尝试解决作业问题。该问题表明收集用户输入的数字,然后用星号排列它们以显示图形,最大数字有四十个星号,其余数字随着数字减少而变小。
_NumberList= []
print("Please type 'quit' to stop entering numbers.")
print("Please type 'print' to view the list that has been entered so far.")
a= True
while a:
_number= input("Please enter a number or make a selection. ")
if _number.isdigit():
_number=int(_number)
_NumberList.append(_number)
elif _number.isalpha():
_number= _number.lower()
if _number== 'quit':
a= False
if _number== 'print':
print(_NumberList)
else:
print("Please use digits to enter a number.")
print("For exmaple: 'ten' should be typed at '10'")
else:
print("Invalid entry.")
_NumberList.remove(max(_NumberList))
for i in range(len(_NumberList)):
_NumberList.remove(max(_NumberList))
但是,我不确定如何利用数值数据找到给定的比例。到目前为止,我已经考虑过使用.pop函数了,但到目前为止它并没有多大意义。我考虑让它们上升一步,但同样,这似乎不合逻辑,程序可以运行超过四十个数字。我知道我需要利用一个循环,因此最后是for循环,但我不确定如何从那里继续。
答案 0 :(得分:3)
你的变量名_NumberList
会让我的眼睛受伤,所以我会称之为number_list
largest_number = max(number_list)
scale_factor = 40 / largest_number
scaled_number_list = [int(x * scale_factor) for x in number_list]
for scaled_number in scaled_number_list:
print('*' * scaled_number)