为了账号安全,请及时绑定邮箱和手机立即绑定

如何在使用多个 if 语句时存储数字?

如何在使用多个 if 语句时存储数字?

弑天下 2022-12-27 14:50:01
您应该将可选匹配项移动到一个捕获组中:import pandas as pddata = """\2930 Beverly Glen Circle Los Angeles435 S. La Cienega Blvd. Los Angeles12224 Ventura Blvd. Studio City9570 Wilshire Blvd. Beverly Hills26025 Pacific Coast Hwy. Malibu""".split('\n')df = pd.DataFrame(data)print(df)cities = ['Los Angeles', 'Studio City', 'Beverly Hills','Malibu']c = '|'.join(cities)pat = fr'(.*?)\s({c})'                     # fixed pattern with f and rdf = df[0].str.extract(pat,expand=True)print(df)输出:                          0              10  2930 Beverly Glen Circle    Los Angeles1   435 S. La Cienega Blvd.    Los Angeles2       12224 Ventura Blvd.    Studio City3       9570 Wilshire Blvd.  Beverly Hills4  26025 Pacific Coast Hwy.         Malibu分享编辑跟随于 2020 年 6 月 4 日 1
查看完整描述

1 回答

?
婷婷同学_

TA贡献1844条经验 获得超8个赞

这是固定版本。我建议你再做一些工作:)


from random import choice


t = ["rock", "paper", "scissors"]


tie = 0

lose = 0

win = 0


for i in range(0, 10):

    print("1... 2... 3... go!")


    # you need to refresh these variables on every for iteration

    computer = choice(t)

    player = None


    # if you're using while to make sure player inputs, that's the only thing that needs

    # to be within the while loop

    while not player:

        player = input("rock, paper, scissors: ")

    print("Computer: ", computer)

    print("User: ", player)


    # I would look for a way to simplify determining the winner

    if player == computer:

        # tie += 1 is the same as tie = tie + 1

        tie +=1

        print("Tie!")

    elif player == "rock":

        if computer == "paper":

            lose += 1

            print("You lose!")

        else:

            win += 1

            print("You win!")

    elif player == "paper":

        if computer == "scissors":

            lose += 1

            print("You lose!")

        else:

            win += 1

            print("You win!")

    elif player == "scissors":

        if computer == "rock":

            lose += 1

            print("You lose!")

        else:

            win += 1

            print("You win!")

    else:

        print("That's not a valid play. Check your spelling!")



print("Final Tally")

print("************")

print("User Wins: ", win)

print("Computer Wins: ", lose)

print("Ties: ", tie)


if tie > win and tie > lose:

    print("It's a tie!")

elif win > tie and win > lose:

    print("You won!")

else:

    print("The computer won!")

更新:显然我无事可做。好吧,这是一种简化获胜条件的直接方法。


    win_condition_rock = player == 'rock' and computer == 'scissors'

    win_condition_paper = player == 'paper' and computer == 'rock'

    win_condition_scissors = player == 'scissors' and computer == 'paper'


    if player == computer:

        # tie += 1 is the same as tie = tie + 1

        tie +=1

        print("Tie!")


    elif any([win_condition_paper, win_condition_scissors, win_condition_rock]):

        win += 1

        print('You win')


    else:

        lose += 1

        print('You lose')

更新 2:这是对有效输入的检查


    while player not in t:

        player = input("rock, paper, scissors: ").lower()

        if player not in t:

            print('Invalid input')


查看完整回答
反对 回复 2022-12-27
  • 1 回答
  • 0 关注
  • 67 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信