我正在做一个Python编程课程,我一直在努力探索如何实现它。我写了一些代码,我正在尝试修复弹出的任何错误,但我感到困惑,没有解决任何问题,这就是为什么我转向你们。我很感激当你看到我到目前为止写的东西时弹出的任何想法和建议。我特别难以弄清楚如何做最后一部分,我必须得到高于或低于2美元的价值。
我正在做这个练习:
创建一个更改计数游戏,让用户输入正好两美元所需的硬币数量。实现一个Python程序,提示用户输入5c硬币,10c硬币,20c硬币,50c硬币,1美元硬币和2美元硬币。如果输入的那些硬币的总价值等于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()
答案 0 :(得分:0)
就风格问题而言,为什么不把所有硬币都放在一个清单中:
coin_list = [five, ten, twenty, fifty, one_dollar, two_dollar]
show_change(coin_list)
calculate_value(coin_list)
CalculateAndPrint(coin_list)
注意:您需要更改上述功能的def
。
答案 1 :(得分:0)
函数calculate_value确实返回一个值,但它根本没有分配
return_dollar = calculate_value(five,ten,twenty,fifty,one_dollar,two_dollar)
并且您必须将该值传递给CalculateAndPrint。
CalculateAndPrint(return_dollar)
您还必须更改CalculateAndPrint的定义:
def CalculateAndPrint(dollar):
我没有在这台PC上安装python 3.0,所以我无法测试你所有的程序,但这是我现在能看到的两个问题。