I'll admit it, I am very new to python and need some help. I am trying to convert a very simple calculator from c++ to python. Here is the code so far:
x = 0
y = 0
sign = '+'
def getnum(prompt, number):
number = input(prompt)
def getsign(prompt, sign):
sign = raw_input(prompt)
print sign
def calc(string, number1, number2, sign):
print string
print " "
if sign == '+' or 'plus':
a = x + y
elif sign == 'x' or '*' or 'times':
a = x * y
elif sign == '/' or 'divided by':
a = x / y
elif sign == '-' or 'minus':
a = x - y
print string, a
getnum("Enter first number: ", x)
getnum("Enter second number: ", y)
getsign("Enter sign: ", sign)
calc("The answer is: ", x, y, sign)
print x
print y
print sign
The problem with the functions. At the end, I get this:
The answer is: 0
0
0
+
I can't seem to get the two numbers at the end to change.
calcfunction has noreturn.xandydon't appear on the left side of=statements (except for their initial values). Unlessxoryappear on the left side of=, their value cannot possibly change. Can you list some of the tutorials you've done? Perhaps we can suggest better tutorials that cover the=statement better than the ones you've tried.getnum()to change the values ofxandy. My understanding of a function is when I rungetnum("Enter a number", x)that the variablepromptwill be replaced with"Enter a number"and that the variablenumberwill be replaced with the variablex. Is my thinking wrong or did I mess up my code or something. When it comes to tutorials, I pretty much google my question, and this is the first one I cannot find an answer to anywhere.stringbecause it is a standard python module you can import.getnum("Enter first number: ", x)can not change the value ofx. Period. Please identify what tutorials you've been using. They are misleading you about how function arguments work. For some reason. You need to be given better tutorials to learn from.