0
我正在創建一個程序,你輸入密碼,你可以玩遊戲。在我的定義riddle()
之一中,它告訴我d1
,d2
,d3
,d4
和d5
在它們被定義之前被引用,但據我所知它們已被定義。此外,當這仍然有效時,我試圖解決一個任務會讓它說它已經完成,但是當我完成一個任務時,它仍然說1是不完整的,等等。我需要解決這兩個問題。Python 3,IDLE說變量沒有被定義,但它是
def riddle():
d1 = 'n'
d2 = 'n'
d3 = 'n'
d4 = 'n'
d5 = 'n'
def compcheck():
print('There are 5 tasks to complete. Enter a number to see task.')
if d1 in ('y'):
t1 = 'Completed.'
if d2 in ('y'):
t2 = 'Completed.'
if d3 in ('y'):
t3 = 'Completed.'
if d4 in ('y'):
t4 = 'Completed.'
if d5 in ('y'):
t5 = 'Completed.'
if d1 in ('n'):
t1 = 'Incomplete.'
if d2 in ('n'):
t2 = 'Incomplete.'
if d3 in ('n'):
t3 = 'Incomplete.'
if d4 in ('n'):
t4 = 'Incomplete.'
if d5 in ('n'):
t5 = 'Incomplete.'
print ('1 is ' + t1)
print ('2 is ' + t2)
print ('3 is ' + t3)
print ('4 is ' + t4)
print ('5 is ' + t5)
def solve():
compcheck()
if d1 and d2 and d3 and d4 and d5 in ['y']:
print ('The password is 10X2ID 4TK56N H87Y8G.')
tasknumber = input().lower()
if tasknumber in ('1'):
print('Fill in the blanks: P_tho_ i_ a c_d_ng lan_u_ge. (No spaces. Ex: ldkjfonv)')
task1ans = input().lower()
if task1ans in ['ysoinga']:
d1 = 'y'
solve()
if tasknumber in ('2'):
print('Is the shape of a strand of DNA: A): a Lemniscate, B): a Hyperboloid, C): a Double Helix, or D): a Gömböc.')
task2ans = input().lower()
if task2ans in ['c']:
d2 = 'y'
solve()
if tasknumber in ('3'):
print ('What is the OS with a penguin mascot?')
task3ans = input().lower()
if task3ans in ('linux'):
d3 = 'y'
solve()
if tasknumber in ('4'):
print('')
if tasknumber in ('5'):
print('')
solve()
+1 for'nonlocal',不過值得注意的是它在Python 2中不可用(這不是對於Python 3標籤問題的問題)。我也會超出你的意思,說'string2'中的大部分'string1'或[string2]調用中的'string1都是不適當的,即使它們在工作的地方。第一個表達式檢查'string1'是否是'string2'的一個子字符串,而第二個表達式檢查string1是否等於列表中的任何字符串(其中只有一個字符串,這是進行相等性檢查的一種尷尬方法)。要測試字符串是否相等,只需使用'=='。 – Blckknght
@Blckknght:只是編輯所有。:-) –