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_Python 3.x_Coin Change - Fatal编程技术网

Python 硬币计数游戏

Python 硬币计数游戏,python,python-3.x,coin-change,Python,Python 3.x,Coin Change,我正在学习Python编程课程,我一直在努力思考如何实现这一目标。我已经写了一些代码,我试图修复任何弹出的错误,但我感到困惑,没有解决任何问题,这就是为什么我求助于你们。如果你看到我迄今为止所写的东西,能有任何想法和建议,我将不胜感激。我特别不知道如何做最后一部分,我必须得到的价值是高于或低于2美元 我在做这个练习: 创建一个零钱计数游戏,让用户输入所需的硬币数量,使正好两美元。实现一个Python程序,提示用户输入一些5c硬币、10c硬币、20c硬币、50c硬币、1美元硬币和2美元硬币。如果输

我正在学习Python编程课程,我一直在努力思考如何实现这一目标。我已经写了一些代码,我试图修复任何弹出的错误,但我感到困惑,没有解决任何问题,这就是为什么我求助于你们。如果你看到我迄今为止所写的东西,能有任何想法和建议,我将不胜感激。我特别不知道如何做最后一部分,我必须得到的价值是高于或低于2美元

我在做这个练习:

创建一个零钱计数游戏,让用户输入所需的硬币数量,使正好两美元。实现一个Python程序,提示用户输入一些5c硬币、10c硬币、20c硬币、50c硬币、1美元硬币和2美元硬币。如果输入的硬币总价值等于两美元,那么程序应该祝贺用户赢得游戏。否则,程序应该显示一条消息,通知总数不完全是两美元,并显示该值高于或低于两美元的程度

更新:我对上一个函数做了一些修改,效果非常好

#Global Variables

v_five = float(0.05)
v_ten = float(0.10)
v_twenty = float(0.20)
v_fifty = float(0.50)
v_one_dollar = int(1)
v_two_dollar = int(2)
dollar = 0

def main():

    """The main function defines the variables that are needed by taking input
    from the user. The main() function is calling all the other functions one 
    by one to execute their intended commands and give the results"""

    intro() #Displays the rules of the game

    #Takes input from the user. One input per denomination
    five=float(input(" Enter number of FIVE CENT coins: "))
    ten=float(input(" Enter number of TEN CENT coins: "))
    twenty=float(input(" Enter number of TWNETY CENT coins: "))
    fifty=float(input(" Enter the number of FIFTY CENT coins: "))
    one_dollar=int(input(" Enter the number of ONE DOLLAR coins: "))
    two_dollar=int(input(" Enter the number of TWO DOLLAR coins: "))

    #Shows what the user entered
    show_change(five,ten,twenty,fifty,one_dollar,two_dollar)
    #Converts the value of the total into dollars and cents from 
    #what the user has entered
    calculate_value(five,ten,twenty,fifty,one_dollar,two_dollar)
    #Calculates and Prints the total along with what the final number
    #was
    #CalculateAndPrint(five,ten,twenty,fifty,one_dollar,two_dollar)
    CalculateAndPrint(dollar)
def intro():

    """This function simply prints out the instructions for the user"""

    print("")
    print(" Welcome to the Coin Change game!")
    print(" Enter a number for each denomination below")
    print(" Your total should be $2 and no more.")
    print(" Good Luck!\n")   

def show_change(five,ten,twenty,fifty,one_dollar,two_dollar):

    """This function shows what the user has entered after taking input from
    the user"""

    print("")
    print(" You entered: \n\n {} five cent(s) \n {} ten cent(s) \n {} twenty cent(s) \n {} fifty cent(s) \n {} one dollar \n {} two dollar coins".format(five,ten,twenty,fifty,one_dollar,two_dollar))

def calculate_value(five,ten,twenty,fifty,one_dollar,two_dollar):

    """This function will convert the entered values into cents so that they
    can be calculated and checked if they exceed the $2 amount."""

    fiveAmount = v_five * five
    tenAmount = v_ten * ten
    twentyAmount = v_twenty * twenty
    fiftyAmount = v_fifty * fifty
    oneAmount = v_one_dollar * one_dollar
    twoAmount = v_two_dollar * two_dollar

    global dollar
    dollar = fiveAmount + tenAmount + twentyAmount + fiftyAmount + oneAmount + twoAmount

    """This function checks whether the total was over or under $2 and displays a 
    win or loose message for the user. Also shows the total that the user entered"""

def CalculateAndPrint(dollar):
    if dollar == 2.00:#Checks if the dollar value being passed from the previous function
        #is 2 or not
        print(" \n Congratulations! You've hit a perfect 2!")
        print("")
    else:
        if dollar < 2.00:
            print(" \n Oops! You were a little under 2!")
            print("")
            print(" Your total was: ", dollar)
        else:
            if dollar > 2.00:
                print(" \n Oh no! You went over 2!")
                print("")
                print(" Your total was: ",dollar)

main()
#全局变量
v_五=浮动(0.05)
v_ten=浮动(0.10)
v_二十=浮动(0.20)
v_五十=浮动(0.50)
一美元=int(1)
二美元=int(2)
美元=0
def main():
“”“main函数定义获取输入所需的变量
main()函数将调用所有其他函数
由一个执行他们想要的命令并给出结果
intro()#显示游戏规则
#接受用户的输入。每种面额一次输入
五=浮动(输入(“输入五分硬币的数量:”)
十=浮动(输入(“输入十美分硬币的数量:”)
二十=浮动(输入(“输入TWNETY分硬币的数量:”)
五十=浮动(输入(“输入五十美分硬币的数量:”)
一美元=整数(输入(“输入一美元硬币的数量:”)
二美元=int(输入(“输入两美元硬币的数量:”)
#显示用户输入的内容
显示零钱(五元、十元、二十元、五十元、一元、二元)
#将总计的值从转换为美元和美分
#用户输入的内容
计算价值(五、十、二十、五十、一美元、二美元)
#计算并打印总数以及最终数字
#是
#计算数字(五,十,二十,五十,一美元,二美元)
计算数据点(美元)
def intro():
“”“此函数仅为用户打印说明”“”
打印(“”)
打印(“欢迎参加硬币兑换游戏!”)
打印(“为下面的每个面额输入一个数字”)
打印(“您的总额应为2美元,不得超过。”)
打印(“祝您好运!\n”)
def显示零钱(五美元、十美元、二十美元、五十美元、一美元、二美元):
“”“此函数显示用户从中获取输入后输入的内容。”
用户“”
打印(“”)
打印(“您输入了:\n\n{}五美分\n{}十美分\n{}二十美分\n{}五十美分\n{}一美元\n{}二美元硬币”。格式(五,十,二十,五十,一美元,二美元)
def计算价值(五、十、二十、五十、一美元、二美元):
“”“此函数将输入的值转换为美分,以便
如果超过2美元,则可以计算和检查
五乘五乘五
十元=十元*十元
二十美元=二十美元*二十美元
五万元=v_五十*五十
一美元=一美元*一美元
twoAmount=v_两美元*两美元
全球美元
美元=五美元+十美元+二十美元+五十美元+一美元+两美元
“”“此函数检查总额是否超过或低于$2,并显示
用户的赢或输消息。还显示用户输入的总数“”
def CalculateAndPrint(美元):
如果美元=2.00:#检查是否从上一个函数传递美元值
#是不是2
打印(“\n祝贺你!你打出了完美的2!”)
打印(“”)
其他:
如果美元<2.00:
打印(“\n糟糕!你有点不到2!”)
打印(“”)
打印(“您的总额为:”,美元)
其他:
如果美元>2.00:
打印(“\n哦,不!你超过了2!”)
打印(“”)
打印(“您的总额为:”,美元)
main()

作为一种风格,你为什么不把你所有的硬币都列在一个列表中:

coin_list = [five, ten, twenty, fifty, one_dollar, two_dollar]
show_change(coin_list)
calculate_value(coin_list)
CalculateAndPrint(coin_list)

注意:您需要更改上述函数的定义。

实际上有几个错误:

函数calculate_value返回一个值,但它根本没有赋值

return_dollar = calculate_value(five,ten,twenty,fifty,one_dollar,two_dollar)
您必须将该值传递给CalculateAndPrint

CalculateAndPrint(return_dollar)
您还必须更改CalculateAndPrint的定义:

def CalculateAndPrint(dollar):

我没有在这台电脑上安装python 3.0,所以我无法测试您的所有程序,但这是我现在看到的两个问题。

谢谢您的回答。我不能使用列表,因为讲师要求我们使用到目前为止所学的内容,而我们还没有达到使用数组的部分。在这个主题上已经有一个带有106个问题变体的标签。。。像复制品一样关闭。