I want to be able to print symbols that represent numbers using functions and while loops
Ex:
number = 250
# symbols
C = 100
^ = 50
should print
CC^
While printing one function may work, trying to concatenate two or more print functions lead me to the type error:
TypeError: can't multiply sequence by non-int of type 'function'
number = 251;
def numeral_C(number_par):
while number_par >=100:
numeral_C = number_par / 100
print "C"*numeral_C,
number_par = number_par - numeral_C*100
return ""
def numeral_UpArrow(number_par):
while number_par >=50:
numeral_upArrow = number_par / 50
print "^"*numeral_UpArrow, #error
number_par = number_par - numeral_UpArrow*50
return ""
etruscan_C = str(numeral_C(number))
etruscan_UpArrow = str(numeral_UpArrow(number)) #error
print etruscan_C+etruscan_UpArrow
Traceback (most recent call last):
File "/Applications/Wing IDE/WingIDE.app/Contents/MacOS/src/debug/tserver/_sandbox.py", line 15, in
File "/Applications/Wing IDE/WingIDE.app/Contents/MacOS/src/debug/tserver/_sandbox.py", line 11, in numeral_UpArrow
**TypeError: can't multiply sequence by non-int of type 'function'
I wonder, Is there a way I can print two or more functions without getting the error?
您都在使用&分配该函数内部的函数名称numeric_UpArrow(这是一个函数)。
As the others state, you have a problem in re-using in an assignment something that is already the same name as a function:
def numeral_C(number_par):
while number_par >=100:
#this numeral_C is already a known function name, now youre reusing it as an int
numeral_C = number_par / 100
#you're using print inside a function, not a best practice, but.....
print "C"*numeral_C,
#uncomment the below line to see why the loop is unnecessary
#print '%d = %d - %d' % (number_par - numeral_C*100, number_par, numeral_C*100)
number_par = number_par - numeral_C*100
return ""
#you're printing, rather than returning, making this useless, and you're str()-ing the "" on return
number = 25101;
etruscan_C = str(numeral_C(number))
print
def numeral_c(number_par):
num_c = number_par / 100
return 'C'*num_c
print numeral_c(number)
As you can see by the comments, a simple rename of your function will cure this, and perhaps even a rename of your variable. But onto what I consider the 'larger' problem...
I feel like your original math makes a needless loop. compare the behavior of your numeral_C with my numeral_c: both result in the same numeral-C, but one is more re-usable (by having the 'C' return as a string) and also it lacks a loop and lots of reassignments.
Realistically, I cannot find a case when the loop will happen a second time, based on your reassignment of number_par to subtract the nearest FLOORED 100-multiple. In other words, much of that logic is useless. You could reasonably accomplish that whole function with:
'C'*int(number/100)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.