2016-10-29 57 views
0

我在開始一個while循環試圖生成一個程序,將產生從數字的餅圖或條形圖用戶將輸入的困難。 while循環如何編寫,因此它不會永遠循環?我知道我可以使用for循環來獲得有限的序列,但是我並沒有太多經驗。任何指導都會有幫助!繪圖的Python餅圖/條形圖使用while循環

從SimpleGraphics進口*

打印( 「圖表菜單:1.Pie圖表,圖表2.Bar」)

圖表= INT(輸入(「中輸入1餅圖或2巴圖表:「))

如果圖表== 1:

title = input("Enter the title of the chart: ") 

numSec = int(input("Enter the number of sectors: ")) 

tSum = int(input("Enter the total sum of all sector values: ")) 

gsize = int(input("Ehter the grid size (Between 10 and 400): ")) 

yLabel = input("Enter the label for the Y-Axis:") 

while循環

-t他每個部門的名字

-the值各部門

-draw餅圖

如果圖表== 2:

title = input("Enter the title of the chart: ") 

numCat = int(input("Enter the number of categories: ")) 

grid = int(input("Ehter the grid size (Between 10 and 400): ")) 

while循環

每個類別的-name

- 值對這一類

-draw條形圖

+0

請添加一個縮進你的代碼才能正常格式化。 –

回答

1

您必須給用戶一箇中斷while循環的選項。嘗試是這樣的:

while True: 
    reply = int(input("Chart Menu: 1.Pie Chart, 2.Bar Chart, 3.Exit")) 
    if reply == 3: 
     break 
    elif reply == 1: 
     # do stuff for pie chart 
    elif reply == 2: 
     # do stuff for bar chart 
    else: 
     print("Sorry, I don't understand you. Try again, please.") 
print("Bye!") 

這裏while True是一個無限循環,但break聲明要求的Python立即走出循環(走在這個例子中排隊print("Bye!"))。

參見例如here有關Python中的while循環的更多詳細信息。