Reputation: 43
so i just started learning python in school and I've been practicing it at home so i am new to python. The problem im having is that i am trying to carry over the values from def two() into main(), however when i use {0} to put them in a print or a calculation, it says error and that they don't have any value.
here's my code:
def two():
print("Hello world!")
print("Please enter three numbers")
nam=int(input("Enter the first number: "))
num=int(input("Enter the second number: "))
nom=int(input("Enter the third number: "))
print("So the numbers you have entered are {0}, {1},{2}.".format(nam,nom,num))
def main():
main=two()
inpt=input("what math related problem would you like me to do with them? tell me here: ").capitalize()
if inpt== "Divide":
ans=({0}/{1}/{2})/1
print("{0}, there you go!")
elif inpt== "Times":
ans=(nam*num*nom)/1
print("{1}, there you go!")
And heres what i get from running it:
>>> main()
Hello world!
Please enter three numbers
Enter the first number: 30
Enter the second number: 30
Enter the third number: 30
So the numbers you have entered are 30, 30, 30.
what math related problem would you like me to do with them? tell me here: divide
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
main()
File "C:\Users\chemg\Documents\PracticePY.py", line 40, in main
ans=({0}/{1}/{2})/1
TypeError: unsupported operand type(s) for /: 'set' and 'set'
Upvotes: 0
Views: 978
Reputation: 11625
You can return the values from your previous function and then set them to be variables.
At the end of two()
return nam, num, nom
In main()
where you set main = two()
nam, num, nom = two()
Also, you should rename main
you can override functionality by using namespaces that are already reserved.
You can then do division with these values
ans = nam / num / nom
As it is now these are singletons i.e. - sets containing one element.
You can then use format
to input these into your strings in your print
statements
Upvotes: 1
Reputation: 190
The issue at play here is that you're attempting to apply string formatting to a non-string operation within your main function.
print("So the numbers you have entered are {0}, {1},{2}.".format(nam,nom,num))
The curly braces that you're using in the line above only substittue for values within that single format
function call. When you call ans=({0}/{1}/{2})/1
, you're actually creating three separate sets; that's a different Python data type. You're getting that error because sets are not meant to be divided like your code is attempting to do.
As mentioned in Slayer's answer, your best bet is to return all three variables form your 'two' function:
return nam, num, nom
That way, you can assign them other variable within your main function. I would highly recommend against creating a variable that's named the same thing as your function too. It'll create some very confusing behavior for you.
nam, num, nom = two()
Finally, the line of code that's actually creating the exception can be modified to work.
ans = nam / num / nom
Upvotes: 0
Reputation: 292
In your code here:
ans=({0}/{1}/{2})/1
{0}
, {1}
and {2}
are sets containing one element. You can't divide sets, which is what the error is complaining about.
You will need to pass the values from two()
to main()
. See Slayer's answer for details as to how that is done.
You cannot just use the {0}
tokens where-ever you want because they are read from strings by format()
to format text. Outside a string it's a set containing zero.
Upvotes: 0