小编Luk*_*tra的帖子

使用strtobool在Python3中是/否提示

我一直在尝试为脚本写一个优雅的[y/n]提示符,我将通过命令行运行.我遇到了这个:

http://mattoc.com/python-yes-no-prompt-cli.html

这是我编写的用于测试它的程序(它实际上只涉及将raw_input更改为输入,因为我正在使用Python3):

import sys
from distutils import strtobool

def prompt(query):
    sys.stdout.write("%s [y/n]: " % query)
    val = input()
    try:
        ret = strtobool(val)
    except ValueError:
        sys.stdout.write("Please answer with y/n")
        return prompt(query)
    return ret

while True:
    if prompt("Would you like to close the program?") == True:
        break
    else:
        continue
Run Code Online (Sandbox Code Playgroud)

但是,每当我尝试运行代码时,我都会收到以下错误:

ImportError: cannot import name strtobool
Run Code Online (Sandbox Code Playgroud)

将"从distutils import strtobool"更改为"import distutils"没有帮助,因为引发了NameError:

Would you like to close the program? [y/n]: y
Traceback (most recent call last):
  File "yes_no.py", line 15, in <module>
    if prompt("Would …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

4
推荐指数
2
解决办法
3244
查看次数

将参数传递给 requests.Session.get 会引发 TypeError

我试图理解为什么在我的例子中params传入session.get会引发 TypeError。 response_get返回200响应,同时response_open引发 TypeError。

import requests


class Foo:

    def __init__(self):
        self.session = requests.Session()

    def get(self, url, params=None):
        """Return a Response object."""
        return requests.get(url, params)

    def open(self, url, params=None):
        """Return a Response object using the Session object."""
        return self.session.get(url, params)


if __name__ == '__main__':
    foo = Foo()
    response_get = foo.get('https://estilen.github.io')
    response_open = foo.open('https://estilen.github.io')
Run Code Online (Sandbox Code Playgroud)

错误:

Traceback (most recent call last):
  File "/home/estilen/Dropbox/Python/leapy/foo.py", line 21, in <module>
    response_open = foo.open('https://estilen.github.io')
  File "/home/estilen/Dropbox/Python/leapy/foo.py", line 15, …
Run Code Online (Sandbox Code Playgroud)

python python-3.x python-requests

3
推荐指数
1
解决办法
5426
查看次数

super()在Sublime Text中抛出错误,在PyCharm/Terminal中工作

我作为作业的一部分制作了以下代码.

class Question:
    """Base class for all questions"""

    question_count = 0

    def __init__(self, desc):
        self.desc = desc

        Question.question_count += 1


class MarkovMM(Question):
    def __init__(self, desc, arrival, service):
        super().__init__(desc)
        if self.desc == "Question 2":
            self.answer = round(1 - (1 - (arrival / service)) - ((1 - (arrival / service)) * (arrival / service)), 3)
        elif self.desc == "Question 3":
            self.answer = round(1 / ((service / 60) - (arrival / 60)), 4)

qu2 = MarkovMM("Question 2", 5, 23)
print(qu2.answer)
qu3 = MarkovMM("Question …
Run Code Online (Sandbox Code Playgroud)

python sublimetext

2
推荐指数
1
解决办法
207
查看次数

标签 统计

python ×3

python-3.x ×2

python-requests ×1

sublimetext ×1