I'm writing a python script where I find the average of the sum of three numbers. I am using arguments for inputting the numbers. I need to finish the script by printing out error messages.
If I enter something like:
avg3 3 5
avg3 3 4 5 6
avg3 3 one 5
it needs to print an error telling me how to use it.
Here is the start of the script:
def main():
num1 = int(sys.argv[1])
num2 = int(sys.argv[2])
num3 = int(sys.argv[3])
avg = (num1 + num2 + num3)/3
print("The average of " +str(num1) + " " + str(num2) + " " + str(num3) + " " + "is " + str(round(avg,2)))
Edit:
For checking forcing an input of 3 numbers, and error checking:
def main():
if len(sys.argv) != 4:
print("You did not enter the correct amount of arguments")
else:
try:
num1 = int(sys.argv[1])
num2 = int(sys.argv[2])
num3 = int(sys.argv[3])
avg = (num1 + num2 + num3)/3
print(f"The average of {num1} {num2} {num3} is {round(avg, 2)}")
except (UnboundLocalError, IndexError) as e:
# When you don't pass any args
print("You need to pass arguments")
print(f"($ python avg.py 3 1 2)\n Error = {e}")
except ValueError as e:
# You need to use numbers
print(f"You need to pass arguments as ints")
print(f"(1 or 432. not 1.2, 324.0)\n Error = {e}")
print(f"(1 or 432. not 1.2, 324.0)\n Error = {e}")
See the below for explanation:
len(sys.argv) will be 4 when there are 3 arguments (try print(sys.argv) for exploring this)
You could use a try statement to catch and except two common exceptions when passing args:
def main():
try:
num1 = int(sys.argv[1])
num2 = int(sys.argv[2])
num3 = int(sys.argv[3])
avg = (num1 + num2 + num3)/3
print(f"The average of {num1} {num2} {num3} is {round(avg, 2)}")
except (UnboundLocalError, IndexError) as e:
# When you don't pass any args
print("You need to pass arguments")
print(f"($ python avg.py 3 1 2)\n Error = {e}")
except ValueError as e:
# You need to use numbers
print(f"You need to pass arguments as ints")
print(f"(1 or 432. not 1.2, 324.0)\n Error = {e}")
try will run a block, and except will run when it's listed exceptions are met in the try block (exiting the try loop).
You could also simplify your code to:
nums = [int(x) for x in sys.argv[1:4]]
avg = sum(nums)/3
And access your numbers with
# Same as nums1
nums[0]
and change the length of incoming arguments to anything >= 1 (if you want):
def main():
try:
nums = [int(x) for x in sys.argv[1:len(sys.argv)]]
avg = sum(nums)/len(nums)
print(f"The average of {', '.join([str(x) for x in nums])} is {round(avg, 2)}")
except (UnboundLocalError, IndexError, ZeroDivisionError) as e:
# When you don't pass any args
print("You need to pass arguments")
print(f"($ python avg.py 3 1 2)\n Error = {e}")
except ValueError as e:
# You need to use numbers
print(f"You need to pass arguments as ints")
print(f"(1 or 432. not 1.2, 324.0)\n Error = {e}")
If you are talking about catching the one arg as an error, you could simply catch a value error.
def main():
try:
num1 = int(sys.argv[1])
num2 = int(sys.argv[2])
num3 = int(sys.argv[3])
avg = (num1 + num2 + num3)/3
print("The average of " +str(num1) + " " + str(num2) + " " + str(num3) + " " + "is " + str(round(avg,2)))
except ValueError:
print("All your values need to be ints")
This will ensure that anything outside of a number being passed in as an arg would result in the message All your values need to be ints.
This won't catch any other errors, so you will have to catch them separately. For instance, your first example where you only pass 2 values won't work since there is no num3, but I'm assuming that isn't what you are asking about here.
Related
I have the below code and when I type calculate(2, 0, "/") I would like to print "Division with zero" than just the output, when I divide with 0. Any suggestions?
def calculate(num1, num2, operator):
if operator == "/" or operator == 'divide':
output = float(num1) / float(num2) if float(num2) else 0
return output
I'd suggest allowing calculate to raise ZeroDivisionError and having the caller print the error:
def calculate(num1, num2, operator):
if operator == "/" or operator == 'divide':
return num1 / num2
try:
print(calculate(2, 0, "/"))
except ZeroDivisionError:
print("Division with zero")
That way you aren't having one function either return or print depending on the situation -- calculate always computes a result (which might include a ZeroDivisionError) without printing it, and the calling code is always in charge of printing it out.
You can simply add an additional condition handling the case you mentioned.
Example:
def calculate(num1, num2, operator):
if num2 == 0 :
print("Division with zero")
return 0
if operator == "/" or operator == 'divide':
output = float(num1) / float(num2)
return output
def main():
calculate(4, 2, "/") # 2
calculate(2, 0, "/") # 0
if __name__ == "__main__":
main()
I have the following code:
def five_numbers():
my_list = []
for i in range(1, 6):
user_nr = check_if_number_is_1_to_25(input("Number " + str(i) + ": "))
my_list.append(user_nr)
return my_list
def check_if_number_is_1_to_25(number):
if number.isalpha():
print("Enter a number between 1 and 25.")
# Here I want to go back to five_numbers() and the number x (for example number 4)
Now I want to check if the input contains any letters. If it has, I want to print a message and then I want to go back to the number that the user was on earlier. I've tried to return five_numbers() but then the user will start from the beginning.
I appreciate all the help.
Add a keyword arg for num and default it to None:
def five_numbers(num=None):
my_list = []
if num is None:
for i in range(1, 6):
user_nr = check_if_number_is_1_to_25(input("Number " + str(i) + ": "))
my_list.append(user_nr)
else:
# do other stuff with num (4) here...
return my_list
def check_if_number_is_1_to_25(number):
if number.isalpha():
print("Enter a number between 1 and 25.")
five_numbers(4)
You can use a while loop to keep asking the user for a valid input until the user enters one. You should also make the check function raise an exception instead so the caller can catch the exception and retry the input:
def five_numbers():
my_list = []
for i in range(1, 6):
while True:
user_nr = input("Number " + str(i) + ": ")
try:
check_if_number_is_1_to_25(user_nr)
break
except ValueError as e:
print(str(e))
my_list.append(user_nr)
return my_list
def check_if_number_is_1_to_25(number):
if number.isalpha():
raise ValueError('Enter a number between 1 and 25.')
Don't use a for loop, use a while loop with the list length as its condition. Make the check function return a boolean and use it to decide whether to append to the list.
def five_numbers():
my_list = []
while len(my_list) < 5:
user_nr = input("Number {}: ".format(len(my_list)+1))
if check_if_number_is_1_to_25(user_nr):
my_list.append(user_nr)
else:
print("Enter a number between 1 and 25.")
return my_list
def check_if_number_is_1_to_25(number):
return number.isdigit() and (1 <= float(number) <= 25)
This question already has answers here:
"Function ________ at 0x01D57aF0" return in python
(2 answers)
Closed 6 years ago.
I wrote a new function and when I execute it, I get an error:
<function read_grades at 0x000001F69E0FC8C8>
Ok so here is my code:
def add(x, y):
z = x / y * 100
return z
def calc_grade(perc):
if perc < 50:
return "1"
if perc < 60:
return "2"
if perc < 75:
return "3"
if perc < 90:
return "4"
if perc >= 90:
return "5"
def calc_command():
num1 = input("Input your points: ")
num2 = input("Input maximum points: ")
num3 = add(float(num1), float(num2))
grade = calc_grade(num3)
print("This is your result:", str(num3) + "%")
print("Your grade:", grade)
save = open("grades.txt", "r")
read_grades = save.read()
save = open("grades.txt", "w")
save.write(read_grades + grade)
save.close()
def read_grades():
save = open("grades.txt", "r")
read_grades = save.read()
grades = read_grades.split()
save.close()
return grades
while True:
command = input("Input your command: ")
if command == "CALC":
calc_command()
elif command == "EXIT":
break
elif command == "GRADES":
print(read_grades)
elif command == "HELP":
print("These are the commands:\nCALC - Calculates your grade and writes in the file.\nEXIT - Exits the program.\nGRADES - Reads your previous grades.\nHELP - This command. It helps you.")
else:
print("You inputed an invalid command. Type HELP for help.")
This error happens when I execute the read_grades() function or the GRADES command.
For those who marked this question: I did some searching and I didn't find that post and now that i read it i dont understand the answer
That's not a runtime error, you printed a function
print(read_grades)
Try calling it instead
read_grades()
And you override your function here
read_grades = save.read()
So, advice is to not use variable names that conflict with your function names
So i've been trying to create a calculator with more complex structure. The problem that am facing is that i'm trying to create a function that calls another function, i know it seems unneccesary but it will be needed in the future. I have a problem calling the function.
class Calculator:
class Variables:
# Start by defining the variables that you are going to use. I created a subclass because I think is better and
# easier for the code-reader to understand the code. For the early stages all variables are going to mainly
# assigned to 0.
n = 0 # n is the number that is going to be used as the main number before making the math calculation
n_for_add = 0 # n_for_add is the number that is going to added to "n" in addition
n_from_add = 0 # n_from_add is the result from the addition
self = 0
def addition(self):
try:
n = int(input("enter number: ")) # user enters the n value
n_for_add = int(input("What do you want to add on " + str(n) + " ? ")) # user enters the n_for_add value
except ValueError: # if the value is not an integer it will raise an error
print("you must enter an integer!") # and print you this. This will automatically kill the program
self.n = n
self.n_for_add = n_for_add
n_from_add = n + n_for_add # this is actually the main calculation adding n and n_for_add
self.n_from_add = n_from_add
print(str(n) + " plus " + str(n_for_add) + " equals to " + str(n_from_add)) # this will print a nice output
def subtraction():
try:
nu = int(input("enter number: "))
nu_for_sub = int(input("What do you want to take off " + str(nu) + " ? "))
except ValueError:
print("you must enter an integer!")
nu_from_sub = nu - nu_for_sub
print(str(nu) + " minus " + str(nu_for_sub) + " equals to " + str(nu_from_sub))
# this is the same as addition but it subtracts instead of adding
def division():
try:
num = int(input("enter number: "))
num_for_div = int(input("What do you want to divide " + str(num) + " off? "))
except ValueError:
print("you must enter an integer!")
num_from_div = num / num_for_div
print(str(num) + " divided by " + str(num_for_div) + " equals to " + str(num_from_div))
# same as others but with division this time
def multiplication():
try:
numb = int(input("enter number: "))
numb_for_multi = int(input("What do you want to multiply " + str(numb) + " on? "))
except ValueError:
print("you must enter an integer!")
numb_from_multi = numb * numb_for_multi
print(str(numb) + " multiplied by " + str(numb_for_multi) + " equals to " + str(numb_from_multi))
# its the same as others but with multiplication function
def choice(self):
x = self.addition()
self.x = x
return x
choice(self)
Hope it will help you.
Code modified:
class Variables:
def addition(self):
try:
n = int(input("enter number: ")) # user enters the n value
n_for_add = int(input("What do you want to add on " + str(n) + " ? ")) # user enters the n_for_add value
return n + n_for_add
except ValueError:
# if the value is not an integer it will raise an error
pass
def choice(self):
x = self.addition()
self.x = x
return x
objectVariables = Variables()
print objectVariables.choice()
I hope this it may serve as its starting point:
def div(x, y):
return x / y
def add(x, y):
return x + y
def subs(x, y):
return x - y
def do(operation, x, y):
return operation(x, y)
print do(add, 4, 2)
print do(subs, 4, 2)
print do(div, 4, 2)
How to make program like this.???
Input : 4
*
* *
* *
* * * *
I would love to know how to do this, it's been bugging me all week but it was only an extra credit question so my teacher never explained how to do it!! :(
http://i.stack.imgur.com/qlyGu.jpg
I thought this would be fun to try, here is my solution:
PROMPT_MSG = "Please enter a whole number, greater than 1"
PROMPT_MSG_ERR = "Oops! Please try again.."
def validate_input(input):
try:
assert int(input) > 1
return int(input)
except (ValueError, AssertionError) as e:
print PROMPT_MSG_ERR + "\n"
main()
def main():
user_input = raw_input("{0}: ".format(PROMPT_MSG))
valid_input = validate_input(user_input)
if valid_input:
print "{0}*".format(" " * valid_input)
for i in range(0, valid_input)[1:-1]:
print "{0}*{1}*".format(
(" " * (valid_input - i)),
(" " * (i + (i-1))),
)
print " *" * valid_input
if __name__ == '__main__':
main()