简体   繁体   English

在函数之间传递值

[英]Passing On Values Between Functions

Hi i'm working on a program where a user selects an image, then it gets turned into an ASCII version of the picture, which displays in both the console and then gets saved to a notepad .txt file.嗨,我正在开发一个程序,用户选择一个图像,然后将其转换为图片的 ASCII 版本,显示在控制台中,然后保存到记事本 .txt 文件中。 The code below currently does this but I think the way I am using my returns in the functions is messing up the code.下面的代码目前是这样做的,但我认为我在函数中使用返回值的方式弄乱了代码。 Instead of asking for the image and background colour once, it asks for it 3 times before actually drawing out the ASCII image.它不是要求一次图像和背景颜色,而是在实际绘制 ASCII 图像之前要求它 3 次。 Then after it has drawn it, it again asks for a 4th time for the image and background colour, before finally asking for the name to give the .txt file then creating the text file.然后在绘制它之后,它再次要求第四次图像和背景颜色,最后要求名称以给出 .txt 文件,然后创建文本文件。

How do I change this so that it only asks each question once without repeating itself(and what exactly is this happening?).我如何改变它,以便它只问每个问题一次而不重复自己(究竟发生了什么?)。 Thanks谢谢

def inputs():
    image = input('What image file?')

    return (image)

def pictureConvert():

    a = inputs()
    # codefollows

Call the inputs function only once, and store that everywhere.只调用一次输入函数,并将其存储在任何地方。 This ensures that the user will be asked for input only once:这确保用户只会被要求输入一次:

def inputs():
    image = input('What image file?')

    return (image)

def pictureConvert():

    a = inputs()
    # codefollows

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM