2017-10-04 70 views
0
def round1(): 
    global Rod, Jane, Freddy 
    Rod=int(input("please enter Rod's score")) 
    Jane=int(input("please enter Jane's score")) 
    Freddy=int(input("please enter Freddy's score")) 
    print("The loser is") 
    loser=min(Rod, Jane, Freddy) 
    print(loser) 
    round2() 


def round2(): 
    print("are you ready for the next scores?") 


round1() 

我希望最高得分者的前兩位名字能夠在下一輪中輸入到這裏 - 甚至有可能嗎?!請放輕鬆,我不是一個編碼員,只是一個老師試圖盡我所能!Python中的變量從int移動到字符串

+0

請用編程語言標記問題,而不是語言關鍵字。 –

+0

我希望我知道你的意思 – user3631273

回答

0

我會使用一個名爲'字典'的Python數據結構,因爲你想爲一個名字賦值。這樣就很容易刪除'失敗者'。

dic1 = {'Rod': 0, 'Jane': 0, 'Freddy' : 0} 

def round1(): 

    dic1['Rod'] = input("please enter Rod's score") #here we add the input values to the keys 
    dic1['Jane'] = input("please enter Janes's score") 
    dic1['Freddy'] = input("please enter Freddys's score") 


    print("The loser is") 

    loser=min(dic1.get('Rod'), dic1.get('Jane'), dic1.get('Freddy')) 

    for key, value in dic1.items(): #here we loop over the dictionary to find the loser and delete him 
     if value is loser: 
      print(key) #here we print the key of the loser, in this case a String 
      del dic1[key] #here we delete the loser out of the dictionary 

    round2() 

def round2(): 
    print("are you ready for the next scores?") 
    print(dic1) 

round1() 

編輯剛看到你有一個Python3標籤。在這種情況下,據我所知,您可能必須在for循環中用list()填充dic1.items()。這將使它列出(dic1.items())。這是因爲Python3像Python2.7一樣返回視圖而不是列表

0

我們可以傳遞玩家分數和玩家名稱的元組。由於這些啓動比分,我們可以呼籲他們min()因爲如果他們的數字:

def round1(players): 
    loser = min(*players) 
    print("The loser is:", loser[1]) 

    remaining = [player for player in players if player != loser] 

    round2(remaining) 

def round2(players): 
    print(", ".join(player[1] for player in players), "are still in the game.") 
    print("Are you ready for the next scores?") 

rod = int(input("Please enter Rod's score: ")) 
jane = int(input("Please enter Jane's score: ")) 
freddy = int(input("Please enter Freddy's score: ")) 

round1([(rod, 'Rod'), (jane, 'Jane'), (freddy, 'Freddy')]) 

用法

% python3 test.py 
Please enter Rod's score: 13 
Please enter Jane's score: 34 
Please enter Freddy's score: 56 
The loser is: Rod 
Jane, Freddy are still in the game. 
Are you ready for the next scores? 
% 

我怎麼可能讓前兩名經歷只能用分?

我假設你不想讓列表理解分離出剩下的玩家。我們可以擺脫它的方法之一,是通過players一個set代替list

def round1(players): 
    loser = min(*players) 

    print("The loser is:", loser[1]) 

    round2(players - set(loser)) 

def round2(players): 
    print(", ".join(player[1] for player in players), "are still in the game.") 
    print("Are you ready for the next scores?") 

rod = int(input("Please enter Rod's score: ")) 
jane = int(input("Please enter Jane's score: ")) 
freddy = int(input("Please enter Freddy's score: ")) 

round1({(rod, 'Rod'), (jane, 'Jane'), (freddy, 'Freddy')}) 

這是有道理的,因爲有暗示沒有特定的順序。

+0

這真是太棒了!非常感謝! – user3631273

+0

對不起,作爲補充,我怎樣才能使前兩名只使用min?我需要以不同的方式編碼嗎? – user3631273

+0

@ user3631273,我用基於集合的方法擴大了我的答案,以消除列表理解。 – cdlane