Poetry | AttributeError 'Link' object has no attribute 'name' - python-poetry
I want to install packages from poetry.lock file; using poetry install.
However, the majority of packages throw the exact same error, indicating a shared fundamental problem.
What is causing this? What is the standard fix?
Specification:
Windows 10,
Visual Studio Code,
Python 3.8.10 & Poetry 1.1.11,
Ubuntu Bash.
Terminal:
rm poetry.lock
poetry update
poetry install
me#PF2DCSXD:/mnt/c/Users/user/Documents/GitHub/workers-python/workers/composite_key$ poetry update
Updating dependencies
Resolving dependencies... (217.2s)
Writing lock file
Package operations: 55 installs, 8 updates, 0 removals
• Updating pyparsing (3.0.4 -> 2.4.7)
• Updating pyyaml (5.4.1 -> 6.0)
• Installing arrow (1.2.1)
• Installing chardet (4.0.0)
• Updating itsdangerous (1.1.0 -> 2.0.1)
• Updating jinja2 (2.11.3 -> 3.0.2)
• Updating packaging (20.9 -> 21.2)
• Installing text-unidecode (1.3)
• Updating werkzeug (1.0.1 -> 2.0.2)
• Installing binaryornot (0.4.4)
• Installing bokeh (2.4.1): Failed
AttributeError
'Link' object has no attribute 'name'
at ~/.local/share/pypoetry/venv/lib/python3.8/site-packages/poetry/installation/executor.py:632 in _download_link
628│ raise RuntimeError(
629│ "Invalid hashes ({}) for {} using archive {}. Expected one of {}.".format(
630│ ", ".join(sorted(archive_hashes)),
631│ package,
→ 632│ archive.name,
633│ ", ".join(sorted(hashes)),
634│ )
635│ )
636│
• Updating flask (1.1.4 -> 2.0.2)
• Installing jinja2-time (0.2.0)
• Installing poyo (0.5.0)
• Installing python-slugify (5.0.2)
me#PF2DCSXD:/mnt/c/Users/user/Documents/GitHub/workers-python/workers/composite_key$ ls
Dockerfile azure-pipeline-composite_key.yaml compositekey docs poetry.lock pyproject.toml
me#PF2DCSXD:/mnt/c/Users/user/Documents/GitHub/workers-python/workers/composite_key$ poetry install
Installing dependencies from lock file
Package operations: 48 installs, 1 update, 3 removals
• Removing cffi (1.15.0)
• Removing colorama (0.4.4)
• Removing pycparser (2.20)
• Installing bokeh (2.4.1): Failed
AttributeError
'Link' object has no attribute 'name'
at ~/.local/share/pypoetry/venv/lib/python3.8/site-packages/poetry/installation/executor.py:632 in _download_link
628│ raise RuntimeError(
629│ "Invalid hashes ({}) for {} using archive {}. Expected one of {}.".format(
630│ ", ".join(sorted(archive_hashes)),
631│ package,
→ 632│ archive.name,
633│ ", ".join(sorted(hashes)),
634│ )
635│ )
636│
Suggested Solution Failed:
danielbellhv#PF2DCSXD:/mnt/c/Users/dabell/Documents/GitHub/workers-python/workers/data_simulator$ pip install poetry==1.1.7
Defaulting to user installation because normal site-packages is not writeable
Collecting poetry==1.1.7
Downloading poetry-1.1.7-py2.py3-none-any.whl (173 kB)
|████████████████████████████████| 173 kB 622 kB/s
Requirement already satisfied: cachy<0.4.0,>=0.3.0 in /home/danielbellhv/.local/lib/python3.8/site-packages (from poetry==1.1.7) (0.3.0)
Requirement already satisfied: requests-toolbelt<0.10.0,>=0.9.1 in /home/danielbellhv/.local/lib/python3.8/site-packages (from poetry==1.1.7) (0.9.1)
Requirement already satisfied: pkginfo<2.0,>=1.4 in /home/danielbellhv/.local/lib/python3.8/site-packages (from poetry==1.1.7) (1.7.1)
Requirement already satisfied: shellingham<2.0,>=1.1 in /home/danielbellhv/.local/lib/python3.8/site-packages (from poetry==1.1.7) (1.4.0)
Requirement already satisfied: tomlkit<1.0.0,>=0.7.0 in /home/danielbellhv/.local/lib/python3.8/site-packages (from poetry==1.1.7) (0.7.2)
Requirement already satisfied: cachecontrol[filecache]<0.13.0,>=0.12.4 in /home/danielbellhv/.local/lib/python3.8/site-packages (from poetry==1.1.7) (0.12.8)
Requirement already satisfied: html5lib<2.0,>=1.0 in /home/danielbellhv/.local/lib/python3.8/site-packages (from poetry==1.1.7) (1.1)
Requirement already satisfied: poetry-core<1.1.0,>=1.0.3 in /home/danielbellhv/.local/lib/python3.8/site-packages (from poetry==1.1.7) (1.0.4)
Requirement already satisfied: crashtest<0.4.0,>=0.3.0 in /home/danielbellhv/.local/lib/python3.8/site-packages (from poetry==1.1.7) (0.3.1)
Requirement already satisfied: clikit<0.7.0,>=0.6.2 in /home/danielbellhv/.local/lib/python3.8/site-packages (from poetry==1.1.7) (0.6.2)
Requirement already satisfied: keyring<22.0.0,>=21.2.0 in /home/danielbellhv/.local/lib/python3.8/site-packages (from poetry==1.1.7) (21.8.0)
Requirement already satisfied: pexpect<5.0.0,>=4.7.0 in /home/danielbellhv/.local/lib/python3.8/site-packages (from poetry==1.1.7) (4.8.0)
Requirement already satisfied: cleo<0.9.0,>=0.8.1 in /home/danielbellhv/.local/lib/python3.8/site-packages (from poetry==1.1.7) (0.8.1)
Requirement already satisfied: virtualenv<21.0.0,>=20.0.26 in /home/danielbellhv/.local/lib/python3.8/site-packages (from poetry==1.1.7) (20.10.0)
Requirement already satisfied: packaging<21.0,>=20.4 in /home/danielbellhv/.local/lib/python3.8/site-packages (from poetry==1.1.7) (20.9)
Requirement already satisfied: requests<3.0,>=2.18 in /usr/lib/python3/dist-packages (from poetry==1.1.7) (2.22.0)
Requirement already satisfied: msgpack>=0.5.2 in /home/danielbellhv/.local/lib/python3.8/site-packages (from cachecontrol[filecache]<0.13.0,>=0.12.4->poetry==1.1.7) (1.0.2)
Requirement already satisfied: lockfile>=0.9 in /home/danielbellhv/.local/lib/python3.8/site-packages (from cachecontrol[filecache]<0.13.0,>=0.12.4->poetry==1.1.7) (0.12.2)
Requirement already satisfied: pastel<0.3.0,>=0.2.0 in /home/danielbellhv/.local/lib/python3.8/site-packages (from clikit<0.7.0,>=0.6.2->poetry==1.1.7) (0.2.1)
Requirement already satisfied: pylev<2.0,>=1.3 in /home/danielbellhv/.local/lib/python3.8/site-packages (from clikit<0.7.0,>=0.6.2->poetry==1.1.7) (1.4.0)
Requirement already satisfied: webencodings in /home/danielbellhv/.local/lib/python3.8/site-packages (from html5lib<2.0,>=1.0->poetry==1.1.7) (0.5.1)
Requirement already satisfied: six>=1.9 in /usr/lib/python3/dist-packages (from html5lib<2.0,>=1.0->poetry==1.1.7) (1.14.0)
Requirement already satisfied: jeepney>=0.4.2 in /home/danielbellhv/.local/lib/python3.8/site-packages (from keyring<22.0.0,>=21.2.0->poetry==1.1.7) (0.7.1)
Requirement already satisfied: SecretStorage>=3.2 in /home/danielbellhv/.local/lib/python3.8/site-packages (from keyring<22.0.0,>=21.2.0->poetry==1.1.7) (3.3.1)
Requirement already satisfied: pyparsing>=2.0.2 in /home/danielbellhv/.local/lib/python3.8/site-packages (from packaging<21.0,>=20.4->poetry==1.1.7) (2.4.7)
Requirement already satisfied: ptyprocess>=0.5 in /home/danielbellhv/.local/lib/python3.8/site-packages (from pexpect<5.0.0,>=4.7.0->poetry==1.1.7) (0.7.0)
Requirement already satisfied: platformdirs<3,>=2 in /home/danielbellhv/.local/lib/python3.8/site-packages (from virtualenv<21.0.0,>=20.0.26->poetry==1.1.7) (2.4.0)
Requirement already satisfied: distlib<1,>=0.3.1 in /home/danielbellhv/.local/lib/python3.8/site-packages (from virtualenv<21.0.0,>=20.0.26->poetry==1.1.7) (0.3.3)
Requirement already satisfied: filelock<4,>=3.2 in /home/danielbellhv/.local/lib/python3.8/site-packages (from virtualenv<21.0.0,>=20.0.26->poetry==1.1.7) (3.3.2)
Requirement already satisfied: backports.entry-points-selectable>=1.0.4 in /home/danielbellhv/.local/lib/python3.8/site-packages (from virtualenv<21.0.0,>=20.0.26->poetry==1.1.7) (1.1.0)
Requirement already satisfied: cryptography>=2.0 in /usr/lib/python3/dist-packages (from SecretStorage>=3.2->keyring<22.0.0,>=21.2.0->poetry==1.1.7) (2.8)
Installing collected packages: poetry
Successfully installed poetry-1.1.7
danielbellhv#PF2DCSXD:/mnt/c/Users/dabell/Documents/GitHub/workers-python/workers/data_simulator$ pip install poetry-core==1.0.4
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: poetry-core==1.0.4 in /home/danielbellhv/.local/lib/python3.8/site-packages (1.0.4)
danielbellhv#PF2DCSXD:/mnt/c/Users/dabell/Documents/GitHub/workers-python/workers/data_simulator$ poetry install
Installing dependencies from lock file
Package operations: 82 installs, 0 updates, 0 removals
• Installing certifi (2021.5.30): Pending...
• Installing charset-normalizer (2.0.3): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing idna (3.2): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing pyasn1 (0.4.8): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
• Installing charset-normalizer (2.0.3): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing idna (3.2): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing pyasn1 (0.4.8): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
• Installing certifi (2021.5.30): Failed
• Installing charset-normalizer (2.0.3): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing idna (3.2): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing pyasn1 (0.4.8): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
• Installing charset-normalizer (2.0.3): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing idna (3.2): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing pyasn1 (0.4.8): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
JSONDecodeError
• Installing charset-normalizer (2.0.3): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing idna (3.2): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing pyasn1 (0.4.8): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
• Installing charset-normalizer (2.0.3): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing idna (3.2): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing pyasn1 (0.4.8): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
Expecting value: line 1 column 1 (char 0)
• Installing charset-normalizer (2.0.3): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing idna (3.2): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing pyasn1 (0.4.8): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
• Installing charset-normalizer (2.0.3): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing idna (3.2): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing pyasn1 (0.4.8): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
• Installing charset-normalizer (2.0.3): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing idna (3.2): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing pyasn1 (0.4.8): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
396│ if ord0 == 0xfeff:
• Installing charset-normalizer (2.0.3): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing idna (3.2): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing pyasn1 (0.4.8): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
397│ idx += 1
• Installing charset-normalizer (2.0.3): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing idna (3.2): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing pyasn1 (0.4.8): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
• Installing charset-normalizer (2.0.3): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing idna (3.2): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing pyasn1 (0.4.8): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
399│ idx += 3
• Installing charset-normalizer (2.0.3): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing idna (3.2): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing pyasn1 (0.4.8): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
• Installing charset-normalizer (2.0.3): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing idna (3.2): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing pyasn1 (0.4.8): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
401│
• Installing charset-normalizer (2.0.3): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing idna (3.2): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing pyasn1 (0.4.8): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
• Installing charset-normalizer (2.0.3): Failed
JSONDecodeError
Expecting value: line 1 column 1 (char 0)
at /usr/lib/python3/dist-packages/simplejson/decoder.py:400 in raw_decode
396│ if ord0 == 0xfeff:
397│ idx += 1
398│ elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399│ idx += 3
→ 400│ return self.scan_once(s, idx=_w(s, idx).end())
401│
• Installing idna (3.2): Failed
...
• Installing pyasn1 (0.4.8): Failed
...
• Installing urllib3 (1.26.6): Failed
...
Please let me know if there is anything else I can add to post.
In my case (poetry 1.1.6) I solved it by clearing the artifacts cache before poetry install:
rm -rf $HOME/.cache/pypoetry/artifacts/*
This looks to be an active issue relating to poetry. See here - Issue #4085. Some suggest a workaround by downgrading poetry-core down to 1.0.4.
There is an active PR to fix the issue.
Related
Why is my case statement failing despite the condition being true?
I'm working on a kata that tallies scores based on the role of five six sided die. Here is my code so far: def score( dice ) points = [] score = {} dice.each do |n| if score.has_key?(n.to_s.to_sym) score[n.to_s.to_sym] += 1 else score[n.to_s.to_sym] = 1 end end score.each do |k,v| key_int = k.to_s.to_i case key_int when key_int == 1 && (v == 3) points << 1000 when key_int == 6 && (v == 3) points << 600 when key_int == 5 && (v == 3) points << 500 when key_int == 4 && (v == 3) points << 400 when key_int == 3 && (v == 2) points << 300 when key_int == 2 && (v == 3) puts "did I get here" points << 200 when key_int == 1 && (v < 3) points << key_int * v when key_int == 5 && (v < 3) points << key_int * v else puts "Default" end end points end puts score([2, 2, 2, 3, 3]) ==> 200 So at a basic level for this example, what's happening is when my each loop hits the hash I get this condition of: key_int == 2 && (v == 3) and insert it into the code before the case statement, I get a value of true but I never reach the condition of points << 200. For simplification I whittled down the code to this for the case logic. score.each do |k,v| key_int = k.to_s.to_i case key_int when key_int == 2 puts "I reached the condition" else puts "default" end end I still get the default and I never reach the when condition. This is confusing me. What am I possibly doing wrong?
If you specify an object right after case, it will be compared against each when pattern via pattern === object. In this example: case key_int when key_int == 2 # ... end it will compare key_int against key_int == 2 like this: (key_int == 2) === key_int Assuming that key_int is 2, the above becomes: true === 2 which evaluates to false. To use a case expression like an if-elsif expression, you have to omit the initial object: case when key_int == 2 # ... end See Ruby's documentation on the case Expression for another example.
How do you make arrow key event handlers in Ruby/Tk
In Ruby/Tk I'm am currently making a length converter but I want to be able to allow the user to either press the up or down arrow key and it will increase or decrease the number the user inputed into the Entry box. require "tk" require "tkextlib/tile" root = TkRoot.new {title "Length converter"} content = Tk::Tile::Frame.new(root){padding "50 15 50 15"}.grid $input_variable = TkVariable.new $output_variable = TkVariable.new input_frame = Tk::Tile::Labelframe.new(content) {text 'Input'; borderwidth 5; relief "sunken"; width 300; height 200; padding "5 0 5 5"} output_frame = Tk::Tile::Labelframe.new(content) {text 'Conversion'; borderwidth 5; relief "sunken"; width 300; height 200; padding "5 0 5 5"} go_button = Tk::Tile::Button.new(content) {text 'Calculate'command {func1}} input_box = Tk::Tile::Entry.new(content) {width 10; textvariable $input_variable} output_text = Tk::Tile::Label.new(content) {textvariable $output_variablefont TkFont.new('sans 12')} underbutton_text = Tk::Tile::Label.new(content) {text 'is equivalent to'} unit_label = Tk::Tile::Label.new(content) {textvariable $unitfont TkFont.new('sans 12')} $input_dom = TkVariable.new $unit = TkVariable.new feet = Tk::Tile::RadioButton.new(input_frame){ text 'Feet' variable $input_dom value 1 pack('fill'=>'x') } meters = Tk::Tile::RadioButton.new(input_frame){ text 'Meters' variable $input_dom value 2 pack('fill'=>'x') } inches = Tk::Tile::RadioButton.new(input_frame){ text 'Inches' variable $input_dom value 3 pack('fill'=>'x') } centi = Tk::Tile::RadioButton.new(input_frame){ text 'Centimeters' variable $input_dom value 4 pack('fill'=>'x') } miles = Tk::Tile::RadioButton.new(input_frame){ text 'Miles' variable $input_dom value 5 pack('fill'=>'x') } kilometers = Tk::Tile::RadioButton.new(input_frame){ text 'Kilometers' variable $input_dom value 6 pack('fill'=>'x') } $output_dom = TkVariable.new feet = Tk::Tile::RadioButton.new(output_frame){ text 'Feet' variable $output_dom value 1 pack('fill'=>'x') } meters = Tk::Tile::RadioButton.new(output_frame){ text 'Meters' variable $output_dom value 2 pack('fill'=>'x') } inches = Tk::Tile::RadioButton.new(output_frame){ text 'Inches' variable $output_dom value 3 pack('fill'=>'x') } centi = Tk::Tile::RadioButton.new(output_frame){ text 'Centimeters' variable $output_dom value 4 pack('fill'=>'x') } miles = Tk::Tile::RadioButton.new(output_frame){ text 'Miles' variable $output_dom value 5 pack('fill'=>'x') } kilometers = Tk::Tile::RadioButton.new(output_frame){ text 'Kilometers' variable $output_dom value 6 pack('fill'=>'x') } content.grid(:column => 0, :row => 0) input_frame.grid(:column => 0, :row => 0, :columnspan => 5, :rowspan =>6) output_frame.grid(:column => 9, :row => 0, :columnspan => 5, :rowspan =>6) input_box.grid(:column => 0, :row => 7) go_button.grid( :column => 7, :row => 7) output_text.grid(:column => 9, :row => 7) underbutton_text.grid( :column => 7, :row => 9) unit_label.grid(:column => 10, :row => 7) def func1 calculate() unit() end def calculate begin if $input_dom == $output_dom $output_variable.value = $input_variable #Feet to ... elsif $input_dom == 1 && $output_dom == 2 $output_variable.value = (($input_variable/3.2808)*10000000.0).round()/10000000.0 elsif $input_dom == 1 && $output_dom == 3 $output_variable.value = (($input_variable*12.000)*10000000.0).round()/10000000.0 elsif $input_dom == 1 && $output_dom == 4 $output_variable.value = (($input_variable/0.032808)*10000000.0).round()/10000000.0 elsif $input_dom == 1 && $output_dom == 5 $output_variable.value = (($input_variable/0.00018939)*10000000.0).round()/10000000.0 elsif $input_dom == 1 && $output_dom == 6 $output_variable.value = (($input_variable/3280.8)*10000000.0).round()/10000000.0 #Meters to ... elsif $input_dom == 2 && $output_dom == 1 $output_variable.value = (($input_variable*3.2808)*10000000.0).round()/10000000.0 elsif $input_dom == 2 && $output_dom == 3 $output_variable.value = (($input_variable*39.370)*10000000.0).round()/10000000.0 elsif $input_dom == 2 && $output_dom == 4 $output_variable.value = (($input_variable/0.010000)*10000000.0).round()/10000000.0 elsif $input_dom == 2 && $output_dom == 5 $output_variable.value = (($input_variable*0.00062137)*10000000.0).round()/10000000.0 elsif $input_dom == 2 && $output_dom == 6 $output_variable.value = (($input_variable/1000.000)*10000000.0).round()/10000000.0 #Inches to ... elsif $input_dom == 3 && $output_dom == 1 $output_variable.value = (($input_variable*0.083333)*10000000.0).round()/10000000.0 elsif $input_dom == 3 && $output_dom == 2 $output_variable.value = (($input_variable/39.370)*10000000.0).round()/10000000.0 elsif $input_dom == 3 && $output_dom == 4 $output_variable.value = (($input_variable/0.39370)*10000000.0).round()/10000000.0 elsif $input_dom == 3 && $output_dom == 5 $output_variable.value = (($input_variable*0.0000157828281218333)*10000000.0).round()/10000000.0 elsif $input_dom == 3 && $output_dom == 6 $output_variable.value = (($input_variable/39370.0)*10000000.0).round()/10000000.0 #Centimeters to ... elsif $input_dom == 4 && $output_dom == 1 $output_variable.value = (($input_variable*0.032808)*10000000.0).round()/10000000.0 elsif $input_dom == 4 && $output_dom == 2 $output_variable.value = (($input_variable/100.000)*10000000.0).round()/10000000.0 elsif $input_dom == 4 && $output_dom == 3 $output_variable.value = (($input_variable*0.39370)*10000000.0).round()/10000000.0 elsif $input_dom == 4 && $output_dom == 5 $output_variable.value = (($input_variable*0.0000062137119)*10000000.0).round()/10000000.0 elsif $input_dom == 4 && $output_dom == 6 $output_variable.value = (($input_variable/100000.000)*10000000.0).round()/10000000.0 #miles to... elsif $input_dom == 5 && $output_dom == 1 $output_variable.value = (($input_variable*5280.0)*10000000.0).round()/10000000.0 elsif $input_dom == 5 && $output_dom == 2 $output_variable.value = (($input_variable/0.00062137)*10000000.0).round()/10000000.0 elsif $input_dom == 5 && $output_dom == 3 $output_variable.value = (($input_variable*63360.000)*10000000.0).round()/10000000.0 elsif $input_dom == 5 && $output_dom == 4 $output_variable.value = (($input_variable/0.0000062137119)*10000000.0).round()/10000000.0 elsif $input_dom == 5 && $output_dom == 6 $output_variable.value = (($input_variable/0.62137)*10000000.0).round()/10000000.0 #kilometers to... elsif $input_dom == 6 && $output_dom == 1 $output_variable.value = (($input_variable*3280.8)*10000000.0).round()/10000000.0 elsif $input_dom == 6 && $output_dom == 2 $output_variable.value = (($input_variable/0.0010000)*10000000.0).round()/10000000.0 elsif $input_dom == 6 && $output_dom == 3 $output_variable.value = (($input_variable*39370.000)*10000000.0).round()/10000000.0 elsif $input_dom == 6 && $output_dom == 4 $output_variable.value = (($input_variable/0.00001)*10000000.0).round()/10000000.0 elsif $input_dom == 6 && $output_dom == 5 $output_variable.value = (($input_variable*0.62137)*10000000.0).round()/10000000.0 end rescue $output_variable.value = '' end end def unit begin if $output_dom == 1 $unit.value = 'ft' elsif $output_dom == 2 $unit.value = 'm' elsif $output_dom == 3 $unit.value = 'in' elsif $output_dom == 4 $unit.value = 'cm' elsif $output_dom == 5 $unit.value = 'mi' elsif $output_dom == 6 $unit.value = 'km' end rescue $unit.value = '' end end Tk.mainloop
Adding this line (after you've defined $input_variable): $input_variable.value = 0.0 as well as adding the following lines (after you've defined input_box): input_box.bind 'Down', lambda {$input_variable.value = $input_variable - 1.0} input_box.bind 'Up', lambda {$input_variable.value = $input_variable + 1.0} should get you closer to seeing it work. See also this question about determining things like Down and Up.
Simple ruby syntax if statement. Is this possible?
I'm really new to programming and would like to know if this is possible: def lock(a,b,c,d) if (a == 3 || a == 5 || a == 7 && b == 2 && c == 5 || c == 6 && d == 8 || d == 9 || d == 0) "unlocked" else "locked" end If it isn't possible, why? So when I pass in: lock(3,1,1,1) it should be locked. Should I do this differently? Is there a better way?
I would use include? to make it more readable: if ([3,5,7].include?a and b == 2 and [5,6].include?c and [8,9,0].include?d)
You should have to add bracket properly as per your logic. Like this, ((a == 3 || a == 5 || a == 7) && (b == 2) && (c == 5 || c == 6) && (d == 8 || d == 9 || d == 0))
Give an if statement a variable in Ruby
I have this if statement: if value_n_in_f > 0 && value_m_in_f != 1 puts "I: f(x)=#{value_m_in_f}x+#{value_n_in_f}" elsif value_n_in_f == 0 && value_m_in_f != 1 puts "I: f(x)=#{value_m_in_f}x" elsif value_n_in_f < 0 && value_m_in_f != 1 puts "I: f(x)=#{value_m_in_f}x#{value_n_in_f}" elsif value_n_in_f > 0 && value_m_in_f == 1 puts "I: f(x)=x+#{value_n_in_f}" elsif value_n_in_f == 0 && value_m_in_f == 1 puts "I: f(x)=x" elsif value_n_in_f < 0 && value_m_in_f == 1 puts "I: f(x)=x#{value_n_in_f}" end` I have to use this statement really often in other statements and it makes my code unnecessarily long. Obviously if_for_f=if ....... end won't work. Is there any other way? Here's an example what I want it to look like: puts "Now we insert #{value_of_x} in #{if_for_f}" Is there any way I can do something like this? Note that I am absolutly new to this. Thanks in advance, Kaiton
can't I just def a function with its function to run this case statement and call it then when I need it it? Of course: def do_stuff(m, n) if m == 1 if n > 0 then "I: f(x)=x+#{n}" elsif n == 0 then "I: f(x)=x" elsif n < 0 then "I: f(x)=x#{n}" end else if n > 0 then "I: f(x)=#{m}x+#{n}" elsif n == 0 then "I: f(x)=#{m}x" elsif n < 0 then "I: f(x)=#{m}x#{n}" end end end puts do_stuff(1, 1) #I: f(x)=x+1 Or, if compactness is the goal, we can get to this: def do_stuff(m, n) if m == 1 n == 0 ? "I: f(x)=x" : "I: f(x)=x#{sprintf("%+d", n)}" else n == 0 ? "I: f(x)=#{m}x" : "I: f(x)=#{m}x#{sprintf("%+d", n)}" end end ...and then a one liner: def do_stuff(m, n) (m == 1) ? (n == 0 ? "I: f(x)=x" : "I: f(x)=x#{sprintf("%+d", n)}") : (n == 0 ? "I: f(x)=#{m}x" : "I: f(x)=#{m}x#{sprintf("%+d", n)}") end end But your methodology has some problems with zeros and -1: def do_stuff(value_m_in_f, value_n_in_f) if value_n_in_f > 0 && value_m_in_f != 1 puts "I: f(x)=#{value_m_in_f}x+#{value_n_in_f}" elsif value_n_in_f == 0 && value_m_in_f != 1 puts "I: f(x)=#{value_m_in_f}x" elsif value_n_in_f < 0 && value_m_in_f != 1 puts "I: f(x)=#{value_m_in_f}x#{value_n_in_f}" elsif value_n_in_f > 0 && value_m_in_f == 1 puts "I: f(x)=x+#{value_n_in_f}" elsif value_n_in_f == 0 && value_m_in_f == 1 puts "I: f(x)=x" elsif value_n_in_f < 0 && value_m_in_f == 1 puts "I: f(x)=x#{value_n_in_f}" end end do_stuff(1, 0) do_stuff(1,-1) do_stuff(1, 1) do_stuff(0,-1) do_stuff(0, 0) do_stuff(-1, 1) --output:-- I: f(x)=x I: f(x)=x-1 I: f(x)=x+1 I: f(x)=0x-1 #<---HERE I: f(x)=0x #<---HERE I: f(x)=-1x+1 #<---HERE So let's fix that: def get_line_equ(m, b) constant = (b == 0) ? "" : sprintf("%+d", b) # 2 => "+2" case m when 0 xterm = "" constant = b when 1 xterm = "x" when -1 xterm = "-x" else xterm = "#{m}x" end "I: f(x)=#{xterm}#{constant}" end puts get_line_equ(0, 0) puts get_line_equ(0, -1) puts get_line_equ(0, 1) puts get_line_equ(1, 0) puts get_line_equ(1,-1) puts get_line_equ(1, 1) puts get_line_equ(-1, 0) puts get_line_equ(-1, -1) puts get_line_equ(-1, 1) puts get_line_equ(2, 0) puts get_line_equ(2, -1) puts get_line_equ(2, 1) --output:-- I: f(x)=0 I: f(x)=-1 I: f(x)=1 I: f(x)=x I: f(x)=x-1 I: f(x)=x+1 I: f(x)=-x I: f(x)=-x-1 I: f(x)=-x+1 I: f(x)=2x I: f(x)=2x-1 I: f(x)=2x+1 Better? spoiler: The final def is not as efficient as it could be: the first line should be removed and copied to each of the when branches--except for the first. In response to comment: def my_sprintf(str, *numbers) str.gsub(/% .*? [df]/x) do |match| #Looks for %...d or %...f sequences puts match end end my_sprintf("The answer is: %+d or %+d", -2, 3) --output:-- %+d %+d Next: def my_sprintf(str, *numbers) str.gsub(/% .*? [df]/x) do |format_sequ| number_as_str = numbers.shift.to_s p number_as_str if format_sequ[1] == "+" and number_as_str[0] != "-" "+#{number_as_str}" else number_as_str end end end puts my_sprintf("The answer is: %+d or %+d.", -2, 3) --output:-- "-2" "3" The answer is: -2 or +3.
Here's a more compact way to write your case statement. Recall that a <=> b => -1 if a < b, a <=> b => 0 if a == b and a <=> b => -1 if a > b. "I: f(x)=" + case [value_n_in_f <=> 0, value_m_in_f == 1] when [ 1,false] then "#{value_m_in_f}x+#{value_n_in_f}" when [ 0,false] then "#{value_m_in_f}x" when [-1,false] then "#{value_m_in_f}x#{value_n_in_f}" when [ 1,true] then "x+#{value_n_in_f}" when [ 0,true] then "x" when [-1,true] then "x#{value_n_in_f}" end If you wish to demonstrate how the string is built, you could do something like this (with value_n_in_f and value_m_in_f renamed intercept and slope, respectively): "I: f(x)=" + case when slope.zero? intercept.zero? ? "0" : "#{intercept}" else case slope.to_f when 1.0 then "" when -1.0 then "-" else "#{slope}" end + "x" + case intercept <=> 0 when 0 then "" when -1 then "#{intercept}" else "+#{intercept}" end end Note that this permits slope < 0, which is not part of the specification. I tested this for various combinations of intercept and slope: intercept slope string -2.1 4 I: f(x)=4x-2.1 -2.1 -2.2 I: f(x)=-2.2x-2.1 -2.1 0 I: f(x)=-2.1 -2.1 0.0 I: f(x)=-2.1 -2.1 -1 I: f(x)=-x-2.1 -2.1 1.0 I: f(x)=x-2.1 0 4 I: f(x)=4x 0 -2.2 I: f(x)=-2.2x 0 0 I: f(x)=0 0 0.0 I: f(x)=0 0 -1 I: f(x)=-x 0 1.0 I: f(x)=x 0.0 4 I: f(x)=4x 0.0 -2.2 I: f(x)=-2.2x 0.0 0 I: f(x)=0 0.0 0.0 I: f(x)=0 0.0 -1 I: f(x)=-x 0.0 1.0 I: f(x)=x 3 4 I: f(x)=4x+3 3 -2.2 I: f(x)=-2.2x+3 3 0 I: f(x)=3 3 0.0 I: f(x)=3 3 -1 I: f(x)=-x+3 3 1.0 I: f(x)=x+3
A couple of things here: you can put "puts" before the whole if/elsif block and avoid having puts on every line, like so: puts case when (value_n_in_f > 0 && value_m_in_f != 1) then "I: f(x)=#{value_m_in_f}x+#{value_n_in_f}" when (value_n_in_f == 0 && value_m_in_f != 1) then "I: f(x)=#{value_m_in_f}x" end Second, a case statement would be much more readable, like so: def big_compare(value_n_in_f, value_m_in_f) msg = case when (value_n_in_f > 0 && value_m_in_f != 1) then "I: f(x)=#{value_m_in_f}x+#{value_n_in_f}" when (value_n_in_f == 0 && value_m_in_f != 1) then "I: f(x)=#{value_m_in_f}x" when (value_n_in_f < 0 && value_m_in_f != 1) then "I: f(x)=#{value_m_in_f}x#{value_n_in_f}" when (value_n_in_f > 0 && value_m_in_f == 1) then "I: f(x)=x+#{value_n_in_f}" when (value_n_in_f == 0 && value_m_in_f == 1) then "I: f(x)=x" when (value_n_in_f < 0 && value_m_in_f == 1) then "I: f(x)=x#{value_n_in_f}" end end puts big_compare(0, 0)
Rock Paper Scissors in Python.... do i use chained or nested conditional to get this to run?
Am i supposed to create a chained conditional or nested? player1 = raw_input ("?") player2 = raw_input ("?") if (player1 == 'rock' and player2 == 'scissors'): print "Player 1 wins." elif (player1 == 'rock' and player2 == 'rock'): print "Tie" elif (player1 == 'scissors' and player2 == 'paper'): print "Player 1 wins." elif (player2 == 'scissors' and player2 == 'scissors'): print "Tie" elif (player1 == 'paper' and player2 == 'paper'): print "Tie" elif (player1 == 'paper' and player2 == 'scissors'): print "Player 2 wins." elif (player1 == 'rock'and player2 == 'paper'): print "Player 2 wins." elif (player1 == 'paper' and player2 == 'rock'): print "Player 2 wins." elif (player1 == 'scissors' and player2 == 'rock'): print "Player 2 wins." else: print "This is not a valid object selection."
The question should probably be migrated to CodeReview, but until then, I'll put my answer here... Instead of making all those checks nested or chained, you can use or to combine different conditions yielding the same result, e.g. one player winning. Also, the check whether player 1 wins over player 2 is the same as the other way around, so you can put this into a function. This will make you code shorter, more readable and easier to maintain. ROCK, PAPER, SCISSORS = "rock", "paper", "scissors" def valid(s): """ s is a valid input """ return s in [ROCK, PAPER, SCISSORS] def wins(p1, p2): """ p1 wins over p2 """ return (p1 == ROCK and p2 == SCISSORS or p1 == SCISSORS and p2 == PAPER or p1 == PAPER and p2 == ROCK) player1 = raw_input ("Player 1: ").strip().lower() player2 = raw_input ("Player 2: ").strip().lower() if valid(player1) and valid(player2): if wins(player1, player2): print "Player 1 wins." elif wins(player2, player1): print "Player 2 wins." else: print "Tie." else: print "This is not a valid object selection."