如何从先前输入的整数中确定输入的数量?骰子游戏

2024-10-01 00:30:15 发布

您现在位置:Python中文网/ 问答频道 /正文

问题: 安东尼娅和大卫在玩游戏。 每个球员从100分开始。 这个游戏使用标准的六边形骰子,分回合进行。在一轮比赛中,每个选手 掷一个骰子。低掷骰子的玩家失去高骰子上显示的点数。如果 两个牌手掷同一个号码,任何一个牌手都不会丢分。 写一个程序来确定最后的分数。

输入规范 输入的第一行包含整数n(1≤n≤15),即 将被播放。在接下来的n行中,每行都是两个整数:那一轮的安东尼娅掷骰子, 接着是空格,接着是那一轮的大卫掷骰子。每次掷骰都是整数 介于1和6之间(包括1和6)。 输出规格 输出将由两行组成。在第一行,输出Antonia拥有的点数 在所有回合结束后。在第二行,输出David拥有的点数 在所有回合结束后。

我的许多问题之一是使程序列出第一个输入指定的正确输入数。

以下是我目前所掌握的情况:

rounds = input()
score1 = input()[0:3]
score2 = input()[0:3]
score3 = input()[0:3]
score4 = input()[0:3]

game = [score1, score2, score3, score4]

antonia = 100
david = 100


for scores in game:
    roll = game
    a = game[0]
    d = game[2]
    if a > d:
        antonia -= int(d[0])
    elif d < a:
        david -= int(a[2])
    elif a == d:
        break
print(antonia)
print(david)

我知道我只要求了一件事,但是有人能完成这个挑战并解释一下最好的方法吗?


Tags: 程序gameinput整数骰子大卫intdavid
3条回答

显然,我不会为您解决这个类似于家庭作业的任务,而是执行n轮(在您的情况下
n = rounds)您应该使用for循环,就像在程序中使用^{}一样:

rounds = input()
for i in range(rounds):
    ...

这将执行rounds次(0rounds-1)。并且i将是索引。在

一种方法:

rounds = input()

players = [100, 100] 

for round in range(rounds): 
    ri = raw_input() # get the whole line as a string
    # split the input, then convert to int
    scores = [int(i) for i in ri.split()] 
    # compute the players scores
    if scores[0] > scores[1]: players[1] -= scores[0] 
    elif scores[1] > scores[0]: players[0] -= scores[1] 

# print the result
print " ".join(map(str, players)) 
# or simply : 
# print players[0]  
# print players[1]    

这是我最新的回答:

rounds = int(raw_input("Number of rounds: "))
david=[]
antonia=[]


for i in range(rounds):
    score = str(raw_input("Enter Antonia's score, leave a space, \
    enter David's score:"))
    david.append(int(score[0]))
    antonia.append(int(score[2]))

def sum(player):
    s=0
    for i in range(len(player)):
        s+=player[i]
    return s

s1 = sum(david)
s2 = sum(antonia)

for j in range(rounds):
    if david[j] > antonia[j]:
        s1-=david[j]
    elif antonia[j] > david[j]:
        s2-=antonia[j]

print s1
print s2

基本上,当你不知道你有多少次迭代(你不知道循环的数量,你只知道它在1到15之间,我还没有解释),你应该使用类似while i<nr of rounds,或者我们的同事告诉你for i in range(rounds)。在

另外,无论何时接受输入,都应该使用原始的输入而不是输入(在python2.7中)。我不认为[0,3]score1 = input()[0:3]中是必要的。在

相关问题 更多 >