繁体   English   中英

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

[英]How do you determine the number of inputs from an integer in a prior input? Dice game

问题:安东尼娅和大卫正在玩游戏。 每个玩家以 100 分开始。 该游戏使用标准的六面骰子并按轮进行。 在一轮中,每个玩家掷一个骰子。 掷出较低的玩家失去较高骰子上显示的点数。 如果两个玩家都掷出相同的数字,则任何一个玩家都不会失去任何分数。 编写一个程序来确定最终分数。

输入规范 输入的第一行包含整数 n (1 ≤ n ≤ 15),这是将进行的回合数。 在接下来的 n 行中的每一行中,将是两个整数:该轮的 Antonia 的滚动,后跟一个空格,然后是该轮的 David 的滚动。 每卷将是 1 到 6(含)之间的整数。 输出规范输出将由两行组成。 在第一行,输出安东尼娅在所有回合打完后的分数。 在第二行,输出大卫在所有回合结束后的得分。

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

这是我到目前为止所拥有的:

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)

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

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

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

这将执行rounds次( 0 ... rounds-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)

此外,无论何时输入,都应该使用 raw_input 而不是 input (在 python 2.7 中)。 我不认为[0,3]score1 = input()[0:3]是必要的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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