2017-01-18 36 views
-3

私の仕事は、ユーザーが自分のプログラムを提供するすべての入力に対して一意の番号を生成し、その入力と共にこの番号を私がcustomer_referencesというファイルに保存することです。Pythonでファイルの各行に番号を付ける方法

ケース番号:1つの 問題は:私の問題がある....

ケース数:2 問題は:私の電話がある...

私は、ファイルがこのような何かを提示したいですケース番号:3 問題:...... (など)

また、ユーザーがpythonについて知るためには、ケース番号を印刷する必要があります。 私はrandintを使用してみましたが、これはケース番号が別のユーザー

のために繰り返されている可能性がありますので、これは私の未完成のコードで十分に効率的ではありませんでした:

def issue_details(): 

    #prints aplogy message for not being able to find a solution 
    print("Sorry we were unable to provide a suitable solution") 

    #Takes further information on details of issue from the user 
    issue = input("Please explain your issue in as much detail so we can direct you to a technician: ") 


    #opens the file I created to store the customers details to append the issue into it. 
    customer_file = open("Customer_references.txt","a") 

    #inputs the issue into the text file 
    customer_file.write(issue) 

    #Here I need to generate the case number and add it to my file beside the users issue 
    #The case number must always be unique 

    print("Thank you for your cooperation. Contact us on 0844558888 and give the technician your reference number so we can try help you\n", "Your reference is: ",#case_number) 
    customer_file.close() 
+0

したがって、問題の数は全顧客で一意でなければなりません。あなたが書いているファイルにはこれらの問題がすべて含まれていますか?そして、これが宿題であれば、私は先に書き込まれたファイルの最後の行を読んで、そのケース番号を決定し、そこからインクリメントします(ケース1、ケース2など)。宿題や実際のシステムではない場合は、データベース(sqliteを起動)を使用します。 – daveydave400

答えて

1

randintがために十分な大きさではありませんユニークなIDの目的。 uuidモジュールを使用して、UUID番号を生成する必要があります.UUID番号はランダムではありますが、すべての実用的な目的で安全であるとみなすことができます

関連する問題