why is the return of the program not good?
Question:
I’m new to python and there’s a video on Youtube that I watched. I do the exact same code as he but mine doesn’t work and I don’ understand why.
Here’s the code:
MAX_LINES = 3
def deposit():
while True:
amount = input("What would you like to deposit? $")
if amount.isdigit():
amount = int(amount)
if amount > 0:
break
else:
print("Amount must be greater than 0. ")
else:
print("Please enter a number. ")
return amount
def get_number_of_lines():
while True:
lines = input("Enter the number of lines to bet on (1-" + str(MAX_LINES) + ")? ")
if lines.isdigit():
lines = int(lines)
if 1 <= lines <= MAX_LINES:
break
else:
print("Please enter a valid number of lines. ")
else:
print("Please enter a number. ")
return lines
There are 3 problems.
- Unindent amount does not match previous indent. I have no idea what does that mean
- "return" can be used only within a function. As far as I’m concerned I’m using it in the function, copied the first function then pasted it into the second function and somehow it doesn’t work
- "lines" is not defined. What dou you mean it’s not defined, I define it in the first line of the function
https://www.youtube.com/watch?v=th4OBktqK1I
This video’s code what I’m trying to do
I appreciate any help!
I just simply don’t understand why it works in one and not the other
Answers:
- You have one space too much in front of
while True
in function get_number_of_lines()
.
- Yes used in functions to return value
- Because function don’t get inside
while
loop (because of indent problem), lines
is never defined, probably this was the problem.
So try fix indent and run again
I’m new to python and there’s a video on Youtube that I watched. I do the exact same code as he but mine doesn’t work and I don’ understand why.
Here’s the code:
MAX_LINES = 3
def deposit():
while True:
amount = input("What would you like to deposit? $")
if amount.isdigit():
amount = int(amount)
if amount > 0:
break
else:
print("Amount must be greater than 0. ")
else:
print("Please enter a number. ")
return amount
def get_number_of_lines():
while True:
lines = input("Enter the number of lines to bet on (1-" + str(MAX_LINES) + ")? ")
if lines.isdigit():
lines = int(lines)
if 1 <= lines <= MAX_LINES:
break
else:
print("Please enter a valid number of lines. ")
else:
print("Please enter a number. ")
return lines
There are 3 problems.
- Unindent amount does not match previous indent. I have no idea what does that mean
- "return" can be used only within a function. As far as I’m concerned I’m using it in the function, copied the first function then pasted it into the second function and somehow it doesn’t work
- "lines" is not defined. What dou you mean it’s not defined, I define it in the first line of the function
https://www.youtube.com/watch?v=th4OBktqK1I
This video’s code what I’m trying to do
I appreciate any help!
I just simply don’t understand why it works in one and not the other
- You have one space too much in front of
while True
in functionget_number_of_lines()
. - Yes used in functions to return value
- Because function don’t get inside
while
loop (because of indent problem),lines
is never defined, probably this was the problem.
So try fix indent and run again