如何知道是否有python函数在请求输入,如果是,请输入input?[duplicate]

vptzau2j  于 2023-01-18  发布在  Python
关注(0)|答案(2)|浏览(130)
    • 此问题在此处已有答案**:

How can I mock user input (from "input" in 3.x, or "raw_input" in 2.x) for a unit test?(7个答案)
昨天关门了。
假设我有这样一个函数:

import random  
def randomfunc():
    x = random.randint(0,10)
    # some code here which generates a name/text randomly and stores it in a .txt file locally/cloud
    if x%2 == 0:
        y=input("Enter your name:") # I need to enter that name/text exact here
        print("Your name is ",y)
    else:
        print("You got an odd number")

现在,每当我运行这个函数的时候,我希望它检测它是否在请求输入,如果它在请求输入,就输入,否则什么都不做。
记住我不能改变函数内部的任何东西。
我试过这个:

import builtins
from unittest.mock import patch
with patch('builtins.input') as input_mock:
    input_mock.side_effect = [
        'My Name',
    ]
    print(input('Enter your name:'))

但这里的问题是输入是在执行input语句之前预先确定的,在我的例子中,我的输入值将在作为我的输入值的randomfunc()的执行是动态的/它随时间变化时被决定。
此外,当使用wexpect模块时,它会给我OSError:[WinError 6]句柄无效。
请给我提些解决办法。

bakd9h0s

bakd9h0s1#

我相信这就是你想要的

def randomfunc():
    x = random.randint(0, 10)
    if x % 2 == 0:
        y = input("Enter your name:")
        print("Your name is ", y)
    else:
        print("You got an odd number")

def better_randomfunc(user_input):
    with mock.patch('builtins.input') as MockClass:
        MockClass.return_value = user_input
        randomfunc()

这个函数会把randomfunc``input()替换成你自己的一个返回user_input的函数,所以如果没有输入函数,它不会像你要求的那样做任何事情,要使用这个函数,只需调用better_randomfunc

better_randomfunc("My name")

并且预计会出现双空格,如果您想消除它,请使用print("Your name is", y)而不是print("Your name is ", y)
编辑:我稍微改进了一下,这样你就可以根据提示修改了

import random
from unittest import mock

def randomfunc():
    x = random.randint(0, 10)
    if x % 2 == 0:
        y = input("Enter your name: ")
        print("Your name is", y)
    else:
        print("You got an odd number")

def better_input(prompt, user_input):
    print(prompt + user_input)
    return user_input

def better_randomfunc(user_input):
    with mock.patch('builtins.input') as MockClass:
        MockClass.side_effect = lambda prompt: better_input(prompt, user_input)
        randomfunc()

better_randomfunc("My name")
ttcibm8c

ttcibm8c2#

您可以替换内置输入功能

import random

input_values = [1,2,3,4]
input_iterator = iter(input_values)

def randomfunc():
    x = random.randint(0,10)
    if x%2 == 0:
        y=input("Enter your name:")
        print("Your name is ",y)
    else:
        print("You got an odd number")

def custom_input(v):
    global input_iterator
    print('custom input called')
    # input(*args, **kwargs)
    return next(input_iterator)

setattr(__builtins__, 'input', custom_input)

while(True):
    try:
        randomfunc()
    except StopIteration:
        print('done')
        break
You got an odd number
custom input called
Your name is  1
custom input called
Your name is  2
custom input called
Your name is  3
You got an odd number
You got an odd number
You got an odd number
custom input called
Your name is  4
You got an odd number
custom input called
done

相关问题