私はPythonには新しく、テキストメニューを持つ簡単なプログラムを作ろうとしています。プログラム内の関数の使用に慣れています)。だから私は、このプログラムの関数を使用して、ユーザーから第1、第2、および場合によっては第3の数値を取得しようとしています。私はこの関数を再利用できるようにする必要があるので、ユーザーからの番号を得ることができますが、関数内でこれらの変数を使用できるだけで問題はありません。どんな提案も役に立ちます!コードは次のとおりです。関数の外で変数を定義する際に問題がある(Python)
option = 1
while option !=0:
print "\n\n\n************MENU************"
print "1. Counting by one"
print "2. Fibbonacci Sequence"
print "0. GET ME OUTTA HERE!"
print "*" * 28
option = input("Please make a selection: ") #counting submenu
if option == 1:
print "\n\n**Counting Submenu**"
print "1. Count up by one"
print "2. Count down by one"
print "3. Count up by different number"
print "4. Count down by different number"
countingSubmenu = input("Please make a selection: ")
def getNum():
firstNum = input("Please state what number to start at: ")
secondNum = input("Please state what number to end at: ")
if countingSubmenu == 3 or countingSubmenu == 4:
thirdNum = input("Please state what increment you would want to go up by: ")
if option == 1:
getNum()
for x in range(firstNum, secondNum+1):
print x
print "End of test."
関数内で宣言した変数は、その外部に存在しません。これらは 'ローカル変数 'と呼ばれます。変数が関数の外に存在するようにするには、関数の外で変数を宣言する必要があります。 –