Python Forum
program is related to leading zeros
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
program is related to leading zeros
#1
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)
Reply
#2
I only see a digit being printed 3 times: in lines 10 (digit), 14 (digit) and 17 (num).
Reply
#3
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)
Reply
#4
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: "))
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Removing leading whitespaces palladium 1 732 Mar-24-2023, 04:15 PM
Last Post: bowlofred
  Removing leading\trailing spaces azizrasul 8 2,725 Oct-23-2022, 11:06 PM
Last Post: azizrasul
  How to retrieve records in a DataFrame (Python/Pandas) that contains leading or trail mmunozjr 3 1,765 Sep-05-2022, 11:56 AM
Last Post: Pedroski55
  -i option changes sys.path (removes leading empty string '') markanth 6 1,994 Aug-26-2022, 09:27 PM
Last Post: markanth
  remove zeros at the end of a number Frankduc 7 2,181 Feb-25-2022, 03:48 PM
Last Post: Frankduc
  Solving for zeros of an equation! fmitchell17 0 1,842 Apr-05-2021, 07:49 PM
Last Post: fmitchell17
  Although this is a talib related Q it's mostly related to python module installing.. Evalias123 4 5,704 Jan-10-2021, 11:39 PM
Last Post: Evalias123
  Dataframe Removes Zeros JoeDainton123 2 4,404 Sep-17-2020, 12:47 PM
Last Post: scidam
  How to keep leading zeros with pandas? eeps24 1 6,581 May-20-2020, 07:51 PM
Last Post: deanhystad
  Cancelling 'open directory' leading to crash Fairbz_ 1 2,192 May-08-2020, 03:14 PM
Last Post: DPaul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020