Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中创建浮动变量?_Python_Loops_Variables_Python Requests_Floating - Fatal编程技术网

如何在Python中创建浮动变量?

如何在Python中创建浮动变量?,python,loops,variables,python-requests,floating,Python,Loops,Variables,Python Requests,Floating,嗨,我正在努力完成我的计算机科学课程,这是为了测试我的能力(我选择的是一个骰子游戏。我们可以使用资源,所以我来到这里,我需要帮助,因为我必须创建一个评分系统,但我需要它与整数一起更改,以便它可以添加变量以获得最终分数。任何帮助都将非常好!我已附上我的代码如下: import time import random def totalScore(n,s): file = open("total.txt", "a") file.write("name: " + n + ", total: " +

嗨,我正在努力完成我的计算机科学课程,这是为了测试我的能力(我选择的是一个骰子游戏。我们可以使用资源,所以我来到这里,我需要帮助,因为我必须创建一个评分系统,但我需要它与整数一起更改,以便它可以添加变量以获得最终分数。任何帮助都将非常好!我已附上我的代码如下:

import time
import random
def totalScore(n,s):

 file = open("total.txt", "a")
 file.write("name: " + n + ", total: " + str(s) + "\n")
 file.close()
 return

def login():
    while True:
        print("")
        username = input('What is your username? ')
        password = input('What is your password? ')
        if username not in ('User1', 'User2'):
            print("")
            print('Incorrect username, try again')
        if password != 'password':
            print("")
            print('Incorrect password, try again')
            continue
        print("")
        print(f'Welcome, {username} you have been successfully logged in.')
        break
user1 = login()
user2 = login()
print("")
print("")
time.sleep(0.5)
print("               Rules")
print("               -----")
time.sleep(1)
print("• The points rolled on each player’s dice are added to their score.")
time.sleep(2)
print("• If the final total is an even number, an additional 10 points are added to their score.")
time.sleep(2)
print("• If the final total is an odd number, 5 points are subtracted from their score.")
time.sleep(2)
print("• If they roll a double, they get to roll one extra die and get the number of points rolled added to their score.")
time.sleep(2)
print("• The score of a player cannot go below 0 at any point.")
time.sleep(2)
print("• The person with the highest score at the end of the 5 rounds wins.")
time.sleep(2)
print("• If both players have the same score at the end of the 5 rounds, they each roll 1 die and whoever gets the highest score wins (this repeats until someone wins).")
time.sleep(2)
print("")
print("")

round_number = 0
msg = "Press Enter to Start The Round:\n\n"
while True:
    if input(msg).lower() != '':
        continue

    round_number += 1
    print(f"Round Number {round_number}\n")

    print("User1 goes first : ")
    roll_number = 0
    dice3 = 0
    print("")
    msg = "Press Enter to Roll The Dice: \n\n"
    while True:
        if input(msg).lower() != '':
           continue

        roll_number = 1
        dice1 = random.randint(1, 6)
        print(f"1> You got a {dice1}\n")
        input ("Press Enter to Roll Again!")
        roll_number = 2
        dice2 = random.randint(1, 6)
        print(f"2> You got a {dice2}\n")
        if roll_number == 2:
            break
    print("You have used both of your rolls")
    total = dice1 + dice2
    print("your total for this round is " + str(total) + ".")

    if dice1 == dice2:
        input ("Lucky!, you get an additional roll : ")
        roll_number = 3
        dice3 = random.randint(1, 6)
        print(f"Bonus Role> You got a {dice3}\n")
        total = total + dice3
        print("your final total for this round is " + str(total) + ".")

    print("User2 its your turn now : ")
    roll_number = 0
    dice3 = 0
    print("")
    msg = "Press Enter to Roll The Dice: \n\n"
    while True:
        if input(msg).lower() != '':
           continue

        roll_number = 1
        dice1 = random.randint(1, 6)
        print(f"1> You got a {dice1}\n")
        input ("Press Enter to Roll Again!")
        roll_number = 2
        dice2 = random.randint(1, 6)
        print(f"2> You got a {dice2}\n")
        if roll_number == 2:
            break
    print("You have used both of your rolls")
    total = dice1 + dice2
    print("your total for this round is " + str(total) + ".")

    if dice1 == dice2:
        input ("Lucky!, you get an additional roll : ")
        roll_number = 3
        dice3 = random.randint(1, 6)
        print(f"Bonus Role> You got a {dice3}\n")
        total = total + dice3
        print("your final total for this round is " + str(total) + ".")

    msg = "Press Enter to Start a new round! \n"
    round_number
    if round_number == 5:
        break
print("All the rounds are complete")

你必须用一些函数、注释等编写可读的代码。。。 对于你的得分,我在下面的代码中做了一些评论,以帮助你完成这个游戏,但是在添加得分之前,请花时间阅读此代码

随机导入
def计算_分数(分数,总计):
“”“此函数返回更新的分数”“”
#把总数加到分数上
#如果总数为偶数,则得分加10分
#如果总数为奇数,减去5分即可得分
#如果分数为正,则返回分数
#否则返回0
返回0
def read_enter(消息=“按enter”):
“”“此功能阻止用户并强制他按enter”“”
输入时(msg).lower()!='':
持续
def掷骰子(msg=”“):
“”“此函数返回一个随机数骰子”“”
骰子=random.randint(1,6)
打印(msg,end=“”)
打印(f“>你得到一个{dice}\n”)
返回骰子
def player_回合(message=“轮到你了:”,分数=0):
“”“此函数是一个玩家回合,返回更新的分数”“”
打印(消息“\n”)
msg=“按Enter键掷骰子:\n\n”
读取\输入(msg)
骰子1=掷骰子(“1”)
读取\回车(“按回车键再次滚动!”)
骰子2=掷骰子(“2”)
打印(“您已经使用了两个卷”)
总计=骰子1+骰子2
打印(“您这一轮的总数为“+str(总数)+”)
#更新分数
更新的分数=计算分数(分数,总数)
3=0
如果dice1==dice2:
输入(“幸运!你得到一个额外的骰子:”)
骰子3=掷骰子(“奖金角色”)
总计=总计+3
打印(“您这一轮的最终总数为“+str(总数)+”)
#返回使用骰子3添加的更新分数
返回更新的_分数+骰子3
def dice_game():
“”“主功能游戏”“”
msg=“按Enter键开始此轮:\n\n”
分数\用户1=分数\用户2=0
对于范围(5)内的四舍五入编号:
读取\输入(msg)
打印(“轮号”,(轮号+1),“\n”)
#你可以打印分数,让玩家随时更新他们的分数
score\u user1=玩家回合(“user1先走:”,score\u user1)
score\u user2=玩家回合(“现在轮到用户2了:”,score\u user2)
msg=“按Enter键开始新一轮!\n”
打印(“所有轮次均已完成”)
#而两名球员的得分相同
#为玩家1掷骰子并更新分数
#为玩家2掷骰子1并更新分数
#如果它们有相同的模具编号
#继续
#否则就断了
#他曾经是赢家
通过
def main():
骰子游戏
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()

欢迎使用SO,请提供一个答案,以便我们可以帮助您关注问题并解决问题。您可以编辑问题,以显示代码的相关部分,哪些部分不起作用?您具体遇到了什么问题?@Sayse Hi,感谢您表现出兴趣。我正在尝试添加一个评分系统,在该系统中,我可以获得一个名为score to loo的变量p,并在最后将所有总数相加。不幸的是,当我尝试此操作时,最好的结果是打印出上一个数字。@user12303290请编辑您的问题,并再次检查上面的共享代码有什么问题。此外,请阅读以下内容以最小化问题中的代码部分,因为它不可读:您好,您好你必须创建两个分数(score1和score2),并使用一个临时变量来计算当前回合和当前玩家的分数,如果这个临时变量为负,你必须重置为0,然后你可以将这个临时分数与玩家分数(score1或score2)相加