亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在使用多個 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 關注
  • 83 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號