I was wondering why this won't work? I'm fairly new to programming and I'm learning Python.
def convert(x,y):
while True:
try:
global x
x = int(input("Number: "))
except ValueError:
print("Make sure it is a number.")
while True:
try:
global y
y = int(input("Number: "))
except ValueError:
print("Make sure it is a number.")
convert(x,y)
Please tell me how to make this work.
Also, the error I get when I run this is name 'x' is parameter and global.
Ok, I fixed it. This is the correct code.
def convert():
while True:
try:
global number
number = int(input("Number: "))
break
except ValueError:
print("Make sure it is a number.")
while True:
try:
global number2
number2 = int(input("Number: "))
break
except ValueError:
print("Make sure it is a number.")
convert()
It's because you're trying to override the parameter x
, but you can't. Here's a related question
To fix this, don't name variables that. You're code is pretty much:
x = 'hi'
x = 5
print(x)
# Why isn't this 'hi'?
By the way, your while loops are going to be running indefinitely. After x = int(input("Number: "))
, you may want to add a break
. Same for the other loop.