我今年11歲,熱衷於學習Python的程序員。語義猜數字遊戲錯誤 - Python3.x
1)我在編程猜數字遊戲,當我問用戶他們是否想再玩一次時,我得到一個語義錯誤(我認爲這是描述它的正確方法),如果輸入「no 「,」n「,」NO「或」N「,if語句仍然執行,導致loop()
函數在計算分數後再次運行。看看下面的圖片(抱歉,關於混亂的窗戶)。
玩一次錯誤:https://i.stack.imgur.com/TsEyw.png
下面是對程序的其它部分的鏈接: https://gist.github.com/anonymous/f9be138e07c569b8721b990293d92314
(我只有8點聲望),但我看着眼前這個片斷:
def play_again():
again = input("\nWould you like to play again? y/n " + prompt)
if again.upper() == "Y" or "YES":
global gu_num
percent = gu_num * 10
score = 100 - percent
highscores = [{round: (score)}]
current_score = {round: (score)}
highscores.append(current_score)
print("Lets see if you can beat your score of " + str(current_score[round]) + ".\nHere we go!")
gu_num = 0
loop()
elif again.upper() == "N" or "NO":
print("Ok then.\nThank you for playing Guess The Number and I hope to see you again soon!\nThis game was created and devoloped by Kiran Patel (me), age 12. If you liked this game or want to talk to me about -->anything<--, please do email me at [email protected] It'll make me happy! Thank you for playing Guess the number with me.\n\n program was developed by Kiran Patel in 2017 ad.")
input("\n\nPress the enter key to exit")
quit()
else:
print("Sorry, I don't understand. Please try again:")
play_again()
2)我的代碼開始處理文件的部分有類似的問題。當我輸入「否」(表達式相同if
)時,程序將執行創建該文件的if語句部分(查看此圖像:file saving result和此圖像:file operations code)。請注意,'文件操作代碼'圖像會打印出文件保存錯誤消息,因爲我沒有在prog'/ files文件夾中寫入燙髮程序。我知道它已經嘗試保存文件,因爲打印出來的錯誤信息(我打算將錯誤信息打印出來)。
3)另外,有沒有人知道#!/usr/bin/python
是什麼意思?我知道它已經被標記出來了,但是我之前看到過它,我想知道它是什麼意思。
與往常一樣,任何幫助將 appreaciated,所以請不要猶豫,增加一些不直接相關的問題,因爲我可能錯過了它(除非它是完全不相關的)。
再次,在此先感謝您的幫助。
從基蘭
你有沒有想過閱讀編程教程? – iehrlich
https://en.wikipedia.org/wiki/Shebang_(Unix) – iehrlich
關於正在執行的條件語句的錯誤分支 - 刪除所有邏輯,並在您的條件中簡單地打印要測試的表達式,例如'again.upper ()'。這應該讓你瞭解爲什麼你的狀況評估不正確。 – iehrlich