我需要帮助编写和实现一个函数到我的当前代码

2024-10-01 22:38:42 发布

您现在位置:Python中文网/ 问答频道 /正文

这段代码将决定你是否想要鸡肉

def main():

    c = 10
    print("Welcome to Roderick's Chikkin and Gravy")
    e = False
    while not e:
        x = input("Would you like some chikkin?: ")
        gravy = input('Would you like some gravy?: ')
        if gravy =="Y" or gravy == "y":
          print('You get some chikkin and gravy!')
          print("Got you some gravy!    Enjoy")
        if x == "Y" or x == "y":
            c = f(c)
            e = True
        elif c == 0:
            e = True
        else:
          e = True
          c=0
          c = f(c)
    print("I hope you enjoyed your meal!")

这段代码告诉输出最后的单词响应

def f(c):

    if c > 0:
        print("Got you some chikkin!  Enjoy")
        return c-1
    else:
        print("No chikkin left.  Sorry")
        return 0

main()

编写一个名为startingChikkin()的简单函数,该函数没有参数,只是询问用户我们从多少chikkin开始。如果用户键入正整数,则返回该值。如果不是,则重复提示,直到它们输入一个正整数,然后返回(请参阅前面有关输入验证的部分)-循环应该在函数内部。在main()程序开始时调用startingChikkin(),并将返回的值存储到您调用的变量中,以跟踪剩余chikkins的数量


Tags: and函数代码youtrueinputifmain
2条回答

在将来,请更清楚地告诉我你尝试了什么(而不是仅仅张贴你的代码)以及你的问题是什么。这是一个简单的问题。以下是您可以从描述中获得的一些要点:

  • 描述清楚地表明,您需要一个名为 startingChikkin不带参数的

  • 它还告诉你函数应该请求输入, 具体来说,它应该一直问,直到你得到正整数。 从这一点你知道,它应该要求输入,你应该 检查输入是否为正整数,如果不是,则继续询问 输入

  • 它还告诉您函数应该返回这个正值 整数和startingChikkin函数应该有一个循环。 循环是必需的,因为您希望不断请求用户输入 直到它是正确的

  • 最后它告诉您应该从main调用startingChikkin 函数并使用返回值计算剩余 小鸡。这意味着您应该有一个 可用的鸡和一个带有用户想要的鸡的变量, 你应该从startingChikkin得到

从这些观点你可以得出如下结论:

def startingChikkin():
    isPositiveInteger = False
    want = -1
    print("Welcome to Roderick's Chikkin and Gravy")
    while not isPositiveInteger:
        user_input = input("Would you like some chikkin?: ")
        try:
            user_input = int(user_input)
        except:
            print("Invalid Input. Please enter a positive Integer.")
        else:
            if user_input > 0:
                want = user_input
                isPositiveInteger = True
            else:
                print("Invalid Input. Please enter a positive Integer.")
    return want


def main():
    chickens = 10
    want = startingChikkin()
    if want > 0:
        print("Chickens in stock: ", chickens)
        print("Chickens in demand: ", want)
        print("Chickens remaining: ", chickens-want)
    else:
        print("Something went wrong!")

if __name__ == "__main__":
    main()

虽然我完全同意其他评论,但我很无聊,所以我为你的代码设计了一个可行的版本。这并不完全是你想要的,但你应该能够从中学习,如果没有别的

# -*- coding: utf-8 -*-
"""
Created on Thu Oct 10 21:49:42 2019

@author: Tom
"""

def prog():
    c = start()
    print("Welcome to Roderick's Chikkin and Gravy")
    e = True
    while e:
        x = input("Would you like some chikkin?: ").upper()
        gravy = input('Would you like some gravy?: ').upper()
        if gravy =="Y":
          print('You get some chikkin and gravy!')
          print("Got you some gravy!    Enjoy")

        if x == "Y":
            e = False
            if c > 0:
                print("Got you some chikkin!  Enjoy")
                c = c - 1
            else:
                print("No chikkin left.  Sorry")
        else:
            e == False

def start():
    true = True
    while true:
        c = int(input('NO. : '))
        if c < 1:
            print('Error')
        else:
            true = False
    return c

def main():
    prog()

main()

相关问题 更多 >

    热门问题