简体   繁体   中英

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

Along with my latest:

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

You should pass these variables as arguments to your functions

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM