2021年5月3日星期一

Python 3 - Programming 101 - Input validation with limited tools

Hey folks I'm hard time programming an input validation on Python. With very very very limited tools I have to validate a string and if it passes the validation I get to turn it into an integer using int().

I am NOT allowed to use: split() isdigit() isnumeric() def continue break try except

I thought about having all my numeric characters within a list .... say:

numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

and then do this

i = 0

while i == 0:

variable1 = input("Insert a number:\n").strip()  if variable1 characters are not in (numbers):      Request input again      i = 0  else:      Next instruction      variable1 = int(variable1)      i = 1  

How do I make it so that it scans through that bank of characters and test if the input contains those specific characters only. This is for a school project where I have to calculate an electricity bills with different pricing, taxes to apply and discount %s to apply. And so basically all inputs need to be by default strings then it goes to validate if they are the characters 0123456789 and then finally gets converted by simply x = int(x) or y = float(y)

Edit:

accepted = False

while not accepted:

bill = input('Bill amount ').strip()  accepted = True  for c in bill:      if c not in '0123456789':          accepted = False  

print(bill)

Does this seem to do the trick?

https://stackoverflow.com/questions/67377524/python-3-programming-101-input-validation-with-limited-tools May 04, 2021 at 08:59AM

没有评论:

发表评论