简体   繁体   English

我对插入乐谱时打印字符串的问题有点困惑

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

I'm really new to Python and one of the things I'm struggling with is getting my code to run properly, I want it to print the current score every round but I'm having an issue adding the score into the string (Ideally not inline but if I have to I can do).我真的是 Python 的新手,我正在努力解决的一件事是让我的代码正常运行,我希望它每一轮都打印当前分数,但是我在将分数添加到字符串中时遇到了问题(理想情况下不是内联的,但如果必须的话,我可以做到)。 If anyone has any ideas, even if it's not specifically about the problem but instead just making the code overall more efficient, any help would be appreciated.如果有人有任何想法,即使它不是专门针对问题,而是只是使代码整体更高效,我们将不胜感激。


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))

Thanks for any help.谢谢你的帮助。

I have tried making strings using: str = "String here. Score = {}".format(score)我尝试使用以下方法制作字符串: str = "String here. Score = {}".format(score)

Along with my latest:连同我的最新消息:

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

You should pass these variables as arguments to your functions您应该将这些变量作为 arguments 传递给您的函数

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

print_str(5)

As an aside you could use f-strings instead of str.format顺便说一句,您可以使用 f-strings 而不是str.format

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

I have now fixed it and thank you to @CodyKramer for the tip in assigning formatting a string.我现在已经修复了它,并感谢@CodyKramer 在分配字符串格式方面的提示。 Turned out I had an issue where the loop was resetting the variable I found after the string was actually printing the score change.结果我遇到了一个问题,即循环正在重置我在字符串实际打印分数变化后发现的变量。

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

Where in fact it should have been:实际上它应该在哪里:

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