首页 > 解决方案 > 如何通过使用循环打印玩家姓名和常规游戏来使代码更小

问题描述

如果 tosses = 4 输出为用户显示两个钉子抛点,如果 tosses = 5,则输出为用户显示一个钉子抛点。另外,我该如何放置这些块引号,因为当我将任何测试代码放入其中时,它会说代码格式不正确。

'''

import random
question = 0
correct = 5
points = 0
tosses = random.randint(3, 5)
utotal = 0
upoint = 0
cpoint = 0
ctotal = 0
user = "Adarsh"
print(user,"s turn")
while tosses <= correct :
    peg = random.randint(0, 5)
    tosses = tosses + 1
    if peg == 0 :
        upoint = - 15
        utotal = utotal - 15
    elif peg == 1 :
        upoint = 20
        utotal = utotal + 20
    elif peg == 2 :
        upoint = 50
        utotal = utotal + 50
    elif peg == 3 :
        upoint = 10
        utotal = utotal + 10
    elif peg == 4 :
        upoint = 30
        utotal = utotal + 30
    elif peg == 5 :
        upoint = 100
        utotal = utotal + 100
    print("Ring is on Peg", peg, ".", "+", upoint)
print("total points", utotal)
print("Computer's turn")
for i in range(0, 4):
    peg = random.randint(0, 5)
    tosses = tosses + 1
    if peg == 0 :
        cpoint = - 15
        ctotal = ctotal - 15
    elif peg == 1 :
        cpoint = 20
        ctotal = ctotal + 20
    elif peg == 2 :
        cpoint = 50
        ctotal = ctotal + 50
    elif peg == 3 :
        cpoint = 10
        ctotal = ctotal + 10
    elif peg == 4 :
        cpoint = 30
        ctotal = ctotal + 30
    elif peg == 5 :
        cpoint = 100
        ctotal = ctotal + 100
    print("Ring is on Peg", peg, ".", "+", cpoint)
print("total points", ctotal)
if ctotal > utotal:
    print("computer wins")
else:
    print("you win")

''' 测试代码

标签: python

解决方案


下面的脚本应该大致给出你想要的。定义了一个toss(user, tosses)userandtosses作为参数的方法。user是用户名,并且tosses相同的投掷次数,以便在同等条件下运行实验。

import random
# The commented lines below are not used
# question = 0
correct = 5
points = 0
#upoint = 0 -- again not used
#cpoint = 0

def toss(user, tosses):
    print(user,"s turn")
    upoint = total_points = 0 # Initialize so that they can be used below 
    while tosses <= correct :
        peg = random.randint(0, 5)
        tosses = tosses + 1
        if peg == 0 :
            upoint = - 15
            total_points = total_points - 15
        elif peg == 1 :
            upoint = 20
            total_points = total_points + 20
        elif peg == 2 :
            upoint = 50
            total_points = total_points + 50
        elif peg == 3 :
            upoint = 10
            total_points = total_points + 10
        elif peg == 4 :
            upoint = 30
            total_points = total_points + 30
        elif peg == 5 :
            upoint = 100
            total_points = total_points + 100
        print("Ring is on Peg", peg, ".", "+", upoint)
    print("total points", total_points)
    return(total_points)

# moved the tosses' initialisation here before we call
# the corresponding methods

tosses = random.randint(3, 5)
utotal = toss("Arash", tosses)
ctotal = toss("Computer", tosses)

# Your comment can perhaps be placed here
if ctotal > utotal:
    print("computer wins")
else:
    print("you win")

运行示例

Arash s turn
Ring is on Peg 4 . + 30
Ring is on Peg 3 . + 10
Ring is on Peg 2 . + 50
total points 90
Computer s turn
Ring is on Peg 2 . + 50
Ring is on Peg 3 . + 10
Ring is on Peg 5 . + 100
total points 160
computer wins

注意:您调用blockquote的链接显示程序执行。如果我理解得很好,您正在考虑将其作为注释放在脚本中的某处,可能是在打印发生的最后。


推荐阅读