我是一名編程新手,我選擇Python作爲第一語言,因爲它很簡單。但我很困惑這裏的代碼:爲什麼我們在循環之前分配了變量「option」1的值?
option = 1
while option != 0:
print "/n/n/n************MENU************" #Make a menu
print "1. Add numbers"
print "2. Find perimeter and area of a rectangle"
print "0. Forget it!"
print "*" * 28
option = input("Please make a selection: ") #Prompt user for a selection
if option == 1: #If option is 1, get input and calculate
firstnumber = input("Enter 1st number: ")
secondnumber = input("Enter 2nd number: ")
add = firstnumber + secondnumber
print firstnumber, "added to", secondnumber, "equals", add #show results
elif option == 2: #If option is 2, get input and calculate
length = input("Enter length: ")
width = input("Enter width: ")
perimeter = length * 2 + width * 2
area = length * width
print "The perimeter of your rectangle is", perimeter #show results
print "The area of your rectangle is", area
else: #if the input is anything else its not valid
print "That is not a valid option!"
好吧好吧,我得到的東西都在Option
以下。我只想知道爲什麼我們爲Option=1
賦值,爲什麼我們將它添加到程序的頂部,它的功能是什麼。我們也可以改變它的價值。請讓我用簡單的語言來理解它,因爲我是編程新手。
你無法理解'option = 1'行(一個簡單的變量初始化),你在說:**我把所有的東西都放在「Option」變量的下面**,那怎麼可能? –