Sorry, I'm quite new to python, I know this is probably easy to fix but I have been stuck for ages.
This is my code:
CountFromNum = InputNumber("Which number would you like to count down from? ")
print("This is Joe's Multiple Program Loader, hope you enjoy.")
print("Would you like to:")
print("(a) Count down from a certain number.")
print("(b) Print your name a certain amount of times.")
StartQuestion = input("(c) make a list with a custom length. ")
StartQuestionOptions='abc'
while StartQuestion not in StartQuestionOptions :
StartQuestion=input("Please enter the letter a, b or c: ")
if StartQuestion in StartQuestionOptions :
break
else :
StartQuestion=input("Please enter the letter a, b or c: ")
#Module A: P1
if StartQuestion == 'a':
print("You have selected: Option a")
def InputNumber(message):
while True:
try:
Userinput = int(input(message))
except ValueError:
print("Not a number! Please try again.")
continue
else:
return Userinput
break
CountFromNum = InputNumber("Which number would you like to count down from? ")
#Module B: P1
if StartQuestion == 'b':
print("You have selected: Option b")
Username = input("Please enter your name: ")
# Module B: P2
def InputNumber(message):
while True:
try:
Userinput = int(input(message))
except ValueError:
print("Please enter a number, try again.")
continue
else:
return Userinput
break
#Module B: P3
NameRepeatValue = inputNumber("How many times do you want to repeat your name? ")
while NameRepeatValue > 0:
print (Username)
NameRepeatValue -=1
while NameRepeatValue > 0:
exit()
#Module C: P1
if StartQuestion == 'c':
print("You have selected: Option c")
if StartQuestion == 'abc':
print("Stop trying to break my program.")
I keep getting errors whenever I do something, please help.
The main issue is you're attempting to use a function before it's been defined. Move your definition of InputNumber
to the top of your file; or at least before it's used.
The other fix that you should begin practicing is to wrap all code in functions. All the code that you have at the top level that aren't function definitions should ideally be put into a main
function or something similar. That will solve your problem here, and also ease development later if you start developing using a REPL. By having all that code outside of functions, you're forcing it to run when the file is read, which is sub-optimal in any code that's not a simple test or toy.
Other notes:
You define InputNumber
twice in two different spots, and the first definition only happens if StartQuestion == 'a'
. Don't do this. Conditionally defining variables/functions will make your code prone to NameError
s, since you allow for the possibility of using a function that wasn't defined. Just define it the once at the top.
Variable names should be in lower_snake_case. UpperCamelCase is reserved for class names.
Once this code is working and complete, you can post it over on Code Review, and people can make further suggestions.