2016-03-28 60 views
1

我寫了一個函數,它接受多個用戶輸入並使用輸入的數據。下面的函數是file1返回使用Python中不同文件的某些頁面

def dimensions 
    f = open("file.txt", "a") 
     distance= (raw_input("Enter distance: ")) 
     height= raw_input("Enter height: ")) 
     length= raw_input("Enter length: ")) 
     f.write(fullInfo) 
     f.close() 

以下是file2File2電話file1import此功能dimensions被稱爲像這樣:

def calculation(): 
     print "1- Enter dimensions" 
     print "2- Enter something else" 
     choice = raw_input("") 
     if choice == 1: 
      file1.dimensions 

我這個遇到的問題是,如果用戶不希望進入的尺寸,並錯誤地在菜單中輸入1,他仍然會必須輸入所有三個查詢。

我嘗試導入file1file2並編寫以下代碼,以便它可以返回菜單,但是將兩個文件導入到另一個文件似乎不起作用。這在file1

if distance == 'exit': 
    file2.calculation 

任何幫助或指針編碼,我怎麼可以讓用戶脫離的問題路程,返回菜單,將不勝感激。由於

回答

0

添加一個名爲選項0附加選項,這意味着用戶要重新啓動,並添加一個if條件來檢查,如果用戶輸入0

def dimensions 
    f = open("file.txt", "a") 
    distance= (raw_input("Enter distance: ")) 
    if distance == 0: # or any other value that may seem fit 
      # handle the situation to restart the program 
    height= raw_input("Enter height: ")) 
    length= raw_input("Enter length: ")) 
    f.write(fullInfo) 
    f.close() 
+0

我的問題是,如果處理情況輸入一個值。我不知道該輸入什麼內容,並且調用菜單函數不起作用,因爲它所在的文件已經在調用函數dimension() – Johny

0
  • fullInfo是不是在你發佈的代碼定義。
  • if choice == 1:永遠不會是真的,因爲raw_input("")/choice是一個字符串,需要將其轉換爲int與1進行比較纔有意義。

在下面的代碼片段,dimensions缺少括號和冒號,distance不應該被嵌套/縮進下f = open("file.txt", "a")

def dimensions 
    f = open("file.txt", "a") 
     distance= (raw_input("Enter distance: ")) 

你實際上並不通話除非你加括號的功能。這適用於以下calculation

if distance == 'exit': 
    file2.calculation 

「(...)在彼此(...)導入這兩個文件」被稱爲循環進口或圓形進口。有辦法使它工作,但如果可能的話最好避免它。如果你有興趣,更多關於here

呀,對不起...我很無聊

menu.py

import json 
import os 
import get_dimensions 


def dims_load(dims_json_file): 
    """ you hand this function a filename and it returns a dictionary of dimensions """ 
    with open(dims_json_file, "r") as dims_json: 
     return json.loads(dims_json.read()) 

def dims_save(dims_dict, dims_json_file): 
    """ you hand this function a dictionary and a filename to save the dimensions """ 
    with open(dims_json_file, "w") as dims_json: 
     dims_json.write(json.dumps(dims_dict)) 

def menu(): 
    dimensions_file = "dimensions.json" 
    file_exists = os.path.isfile(dimensions_file) 
    print "1- Enter dimensions" 
    print "2- Load previous dimensions" 
    print "3- Johny's funky function!" 
    choice = raw_input() 
    try: 
     choice = int(choice) 
    except ValueError as e: 
     print "Only numbers can be entered. Error details:", str(e) 
    if choice == 1: 
     dims_dict = get_dimensions.prompt_user() 
     if "exit" in dims_dict.values(): 
      print "User chose to exit get_dimensions()" 
      menu() 
     else: 
      if file_exists: 
       print "overwriting your precious data!" 
      dims_save(dims_dict, dimensions_file) 
    elif choice == 2: 
     if file_exists: 
      print dims_load(dimensions_file) 
     else: 
      print "No dimensions file found. Can't print" 
    elif choice == 3: 
     print "Johny's funky function has yet to see the light of day..." 
    else: 
     print "I have no idea what you want. Make up your mind, human!?" 

menu() 

get_dimensions.py

​​
+0

感謝您的回答。大多數這些錯誤來自我試圖簡化代碼在這裏發佈。我的問題是,我希望用戶能夠在維度方法內回到計算方法(即菜單)。問題是計算通過'import'調用維度,因此我無法以維度導入計算,因此無法返回菜單。 – Johny

+0

我現在正在重寫整個事情嘻嘻 – jDo

+0

@Johny檢查我更新的答案 – jDo

相關問題