0
How to use One function for different Purposes
def Cond_Check(): while True: try: n=input() n=int(n) break except ValueError: print("Input must be in NUMBERS") i want to this type output using the Above function Enter Book Id Enter Customer Id Enter Book Quantity I want i use the above function and used it for book id customer id and book quantity For example the user enter id in string This message should be displayed Enter Book Id: shshhd Input must be In numbers Enter Bool Id:
12 Answers
+ 3
I suppose the function you show here means something like âcondition checkâ. You like to get an input as int and handle exceptions that occurs. This does run in a while loop. If no exception is raised break is used to exit while loop. what you need additional is to return the input value back to manage program flow. Instead of break you can use return and send int back.
May be you want this:
def Cond_Check():
while True:
try:
n=int(input())
#n=int(n)
#break
return n
except ValueError:
print("Input must be in NUMBERS")
+ 2
infinite loop and an input inside it may not be the best combo.
def Cond_Check(n):
while True:
try:
n = int(n)
return n
except ValueError:
return "Input must be in NUMBERS"
print(Cond_Check(input()))
+ 2
Here is an other idea that works without exception handling:
def check(choice):
while True:
inp = input()
if inp.isdigit() and inp in choice:
return inp
res = check('1 2 3')
print('program flow selected ',res)
or even simply:
def check(choice):
while True:
inp = input()
if inp in choice:
return inp
res = check('123')
print('program flow selected ',res)
+ 2
hi, here is a sample of how a dict can be dumped to a jason file in the first step. Second step is to read dict back from file and compare it to original dict.
Using jason is from my point of view the most simple way to serialize data / objects to a file.
import json
books = {
'1': ['Riverboat', 'Sam Gold', 22.50], '2': ['Birds', 'Fred Vargas', 15.99], '3': ['Mountains', 'Carol Brown', 17.00]
}
print('befor writing: \n', books) #
# write dict to json file
with open('books.txt', 'w') as json_file:
json.dump(books, json_file)
# read dict back from json file
with open('books.txt') as f_in:
my_data = json.load(f_in)
print('\nafter reading: \n', my_data) # for test only
# check if original dict is same as dict resored from file
print(books == my_data)
All print statements are only to demonstrate the procedure and can be removed.
https://code.sololearn.com/c6i2Xq4DJvx5/?ref=app
+ 1
How can I use files in my project
0
Use a class
0
No i cant i just have to use function only
0
Yeah thankew Is it good to create 4 four split function for diffrernt palces like i have use for Book id one function for customer id another for conditions another
0
Lothar can i implement my project using Files??
I want to store all data of dictionries in files can i do this? If yes then how i need u r hell
0
Abdul Wahab this splits the string by any given letter. Will this suffice??
import re
n = "a2s481z23b22cc"
print(list(filter(None, re.split("[a-z]", n))))
['2', '481', '23', '22']
0
download python/IDEs, play around in the scripts and save them as .py files.
Or, you know, you can also save code here in sololearn.
0
Thanksssss got it thinking to use this in my project even Our teacher didnot asked to use files đđ