2016-09-21 91 views
0
import random 
import pickle, shelve 
import os 
#import RPi.GPIO as GPIO | Raspberry pi only 
import tkinter 
import sys 
import time 

class Operator(object): 
    global list_name 
    def __init__(self): 

     print("Welcome to Python OS 1.0") 
     print("type 'help' to access help...") # ADD CODE OS.REMOVE("FILE")  
    def CheckDetails(self): 
     if not os.path.isfile('details.dat') : 
      data=[0] 
      data[0] = input('Enter Your Name: ') 
      file= open('details.dat' , 'wb') 
      pickle.dump(data , file) 
      file.close() 
     else : 
      File = open('details.dat' , 'rb') 
      data = pickle.load(File) 
      file.close() 
      user = "" 
      while user != data[0]: 
       input("please enter your username...") 
      print('Welcome Back To Python OS, '+ data[0]) 

    def Help(self): 
     print(""" 
write(sentence) - Prints the typed sentence on the screen 
open(file, mode) - Opens the file and mode such as 'r' 
create(listName) - creates the list, listName 
add(data, listName) - adds the data to listName 
remove(data, listName) - removes the selected data from listName 
       """) 
    def write(self, sentence): 
     print(sentence) 

    @classmethod 
    def create(self): 
     list_name = input("Please enter the list name...") 
     vars()[list_name] = [] 
     time.sleep(1) 
     print("List (" + list_name + ") created") 

    def add(self): 
     data = input("Please specify the data to be added...") 
     list_name += data 

    def remove(self, data, list_name): 
     remove_data = input("Plese specify the data to be removed...") 
     list_name -= data 


def main(): 

    os = Operator() 
    os.CheckDetails() 
    ans = "" 
    ans = ans.lower() 
    while ans != "quit": 
     ans = input() 

     if ans == "write": 
      os.write() 
     elif ans == "help": 
      os.Help() 
     elif ans == "create": 
      os.create() 
     elif ans == "add": 
      os.add() 
     elif ans == "remove": 
      os.remove() 
     elif ans == "quit": 
      break 
     else: 
      print("Sorry, that command does not exist or it will be added into a future update...") 
    print("goodbye...") 
main() 

我試圖做一些簡化的python,但僅在CheckDetails()函數上發生錯誤。我正在酸洗數據(這很好),但在讓用戶檢查他或她的用戶名時發生錯誤是正確的,我已經測試了它,即使我輸入了正確的用戶名,它也會詢問我的用戶名。任何人都可以幫忙嗎?酸洗數據問題

+0

你並沒有使用在while循環中輸入到你的'user'變量 – bunji

回答

0

你有一個while循環會永久執行,因爲你沒有把你的用戶變量賦值給任何東西。 ! 而用戶=數據[0]: 用戶=輸入( 「請輸入您的用戶名...」) 打印( '歡迎回到Python的OS,' +數據[0])

+0

WOW,我很愚蠢,謝謝你發現錯誤 –