簡體   English   中英

我對插入樂譜時打印字符串的問題有點困惑

[英]A little confused about my issues with printing a string while inserting a score

我真的是 Python 的新手,我正在努力解決的一件事是讓我的代碼正常運行,我希望它每一輪都打印當前分數,但是我在將分數添加到字符串中時遇到了問題(理想情況下不是內聯的,但如果必須的話,我可以做到)。 如果有人有任何想法,即使它不是專門針對問題,而是只是使代碼整體更高效,我們將不勝感激。


from random import randint
play =  1 
while play == 1:
    # Get user input & choose value
    player, opponentInt = input("Rock, Paper, or Scissors? (r/p/s)\n"), randint(1,3)
    player = player.lower()
    # Assigning player input to int value
    playerInt = []
    if player == 'r':
        playerStr, playerInt = 'Rock', 1
    elif player == 'p':
        playerStr, playerInt = 'Paper', 2
    elif player == 's':
        playerStr, playerInt = 'Scissors', 3
    # Assigning randint function input to str value
    if opponentInt == 1:
        opponentStr = 'Rock'
    elif opponentInt == 2:
        opponentStr = 'Paper'
    elif opponentInt == 3:
        opponentStr = 'Scissors'
    # Define strings
    def winStr():
        print("You chose {}, and I chose {}. Congratulations! You won! (Score = {})".format(player, opponentStr, score))
    def loseStr():
        print("You chose {}, and I chose {}. Unfortunately, you lost. (Score = {})".format(player, opponentStr, score))
    def drawStr():
        print("You chose {}, and I chose {}. It was a draw. (Score = {})".format(player, opponentStr, score))
    # Give result of game
    score = 0
    if playerInt == []:
        print('Unexpected value, please play again and check spellings.')
    elif playerInt == 1 and opponentInt == 3:
        score = score + 1
        winStr()
    elif playerInt == 3 and opponentInt == 1:
        score = score - 1
        loseStr()
    elif playerInt > opponentInt:
        score = score + 1
        winStr()
    elif playerInt < opponentInt:
        score = score - 1
        loseStr()
    elif playerInt == opponentInt:
        drawStr()
    # Ask user if they would wish to play again
    play = 2
    while play == 2:
        playAgain = input("Would you like to play again? (y/n)  ")
        playAgain = playAgain.lower()
        if playAgain == 'y':
            play = 1
        elif playAgain == 'n':
            play = 0
        else:
            play = 2
            print("Unexpected value...")
# Print score
print("Your score was {}.".format(score))

謝謝你的幫助。

我嘗試使用以下方法制作字符串: str = "String here. Score = {}".format(score)

連同我的最新消息:

def str():
    print("String here. Score = {}".format(score))
str()

您應該將這些變量作為 arguments 傳遞給您的函數

def print_str(score):
    print("String here. Score = {}".format(score))

print_str(5)

順便說一句,您可以使用 f-strings 而不是str.format

def print_str(score):
    print(f'String here. Score = {score}')

我現在已經修復了它,並感謝@CodyKramer 在分配字符串格式方面的提示。 結果我遇到了一個問題,即循環正在重置我在字符串實際打印分數變化后發現的變量。

play = 1
while play == 1:  
    ...
    score = 0
    if ...
        score += 1
        print(score)
    ...

實際上它應該在哪里:

play = 1
score = 0
while play == 1:  
    ...
    if ...
        score += 1
        print(score)
    ...

暫無
暫無

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

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