簡體   English   中英

為什么我的代碼可以在python shell中工作,但是當我雙擊py文件時卻不能工作

[英]Why does my code work in python shell but not when I double click the py file

為什么我的python文件在IDLE中可以正常運行,但是當我雙擊它時卻不起作用。 讓我重新表述一下,即使它們在IDLE中正常運行,我的if / else語句也似乎永遠都不成立。

我什至將所有代碼分解為最簡單的if / else語句進行測試,並確保我沒有丟失任何內容。 這是我分解的代碼。 這是py文件中的確切代碼,再次在IDLE中有效,但是當我雙擊py文件時不起作用

choice = input('letter: ')
if choice == 'a':
    print ('that is an a')
    input('press any key to exit...')
else:
    print('that letter is not an a')
    input('press any key to exit')

btw python v3.2 Windows 7

嘗試添加

choice = choice.strip()

這對我有用

choice = input('letter: ')
choice = choice.strip()
if choice == 'a':
    print ('that is an a')
    input('press any key to exit...')
else:
    print('that letter is not an a')
    input('press any key to exit')

否則,您的input將給您提供字母和換行符, if失敗

input()可能正在獲取一個以行結尾的字符串。

嘗試在輸入之后立即添加print(repr(choice)) ,以查看您正在使用的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM