Python Forum

Full Version: program is related to leading zeros
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
according to me this program output should be [0 0 6 2],but output is [0 0 2],why
#request input from the user
num=eval(input("please enter an integer in the range 0....9999"))
if num<0:
    num=0
if num>9999:
    num=9999
print(end="[")
#extract and print thousand place digit
digit=num//1000
print(digit,end=" ")
num%=1000
#extract and print hundred place digit
digit=num//100
print(digit,end=" ")
num%=10
#remainder is the one place digit
print(num,end=" ")
print("]")
Moderator Larz60+: Added Python tags. Please do this in the future (see help, BBCODE)
I only see a digit being printed 3 times: in lines 10 (digit), 14 (digit) and 17 (num).
don't use eval, use int to convert user input. or think of different approach, that would not require conversion to int, actually it will use that the user input is str
Finally, this looks like homework and printing [ and ] looks like clumsy attempt to visually achieve required result (i.e. list)
You are getting the reminder on line 15 and print it. Not the tens

#request input from the user
num=eval(input("please enter an integer in the range 0....9999"))
if num<0:
    num=0
if num>9999:
    num=9999
print(end="[")
#extract and print thousand place digit
digit=num//1000
print(digit,end=" ")
num%=1000
#extract and print hundred place digit
digit=num//100
print(digit,end=" ")
digit = num // 10 # added
print(digit, end=' ') #added
num%=10
#remainder is the one place digit
print(num,end=" ")
print("]")
You should never use eval() to get a number as an input. It's very dangerous if one's input is a valid Python code. It will be executed
num = int(input("Enter a number between 1 and 9999: "))