相关疑难解决方法(0)

逻辑或运算符的行为不符合预期

def Forest(Health,Hunger):
    print'You wake up in the middle of the forest'
    Inventory = 'Inventory: '
    Squirrel =  'Squirrel'
    while True:
        Choice1 = raw_input('You...\n')
        if Choice1 == 'Life' or 'life':
            print('Health: '+str(Health))
            print('Hunger: '+str(Hunger))
        elif Choice1 == 'Look' or 'look':
            print 'You see many trees, and what looks like an edible dead Squirrel, \na waterfall to the north and a village to the south.'
        elif Choice1 == 'Pickup' or 'pickup':
            p1 = raw_input('Pickup what?\n')
            if p1 == Squirrel:
                if Inventory == …
Run Code Online (Sandbox Code Playgroud)

python if-statement logical-or

0
推荐指数
1
解决办法
168
查看次数

检查多种类型的变量类型不会产生预期的结果

任务:

  1. distance_from_zero使用一个参数定义一个函数.
  2. 具有该功能执行以下操作:
    • 检查它收到的输入类型.
    • 如果类型为intfloat,则函数应返回函数输入的绝对值.
    • 如果类型是任何其他类型,则函数应返回 "Not an integer or float!"

我的回答不起作用:

def distance_from_zero(d):
    if type(d) == int or float:
        return abs(d)
    else:
        return "Not an integer or float!"
Run Code Online (Sandbox Code Playgroud)

python types boolean-logic if-statement

0
推荐指数
2
解决办法
757
查看次数

为什么我的程序不会退出?

好吧,所以我已经尝试了一段时间了,一旦它运行,我似乎找不到关闭程序的方法.

我想要它做的是如果用户选择'no'以下结束程序:

elif y == 'n' or 'no':
    sys.exit(0)
Run Code Online (Sandbox Code Playgroud)

无论我选择什么,都会将程序返回给partOne函数.我尝试过不同的尝试来解决这个问题,比如搬家

partOne()
Run Code Online (Sandbox Code Playgroud)

从程序结束到两个函数之间但是不起作用,因为那时PartTwo函数尚未定义.

感谢您的答复.

import hashlib
import sys

def partOne():
    x = input("\nEnter something to hash: ")
    hash_object = hashlib.sha256(x.encode())
    hex_dig = hash_object.hexdigest()
    print("\nPlain text: ")
    print(x)
    print("\nHashed text: ")
    print(hex_dig)
    print("\nYour password: ")
    print(hex_dig[::9])
    partTwo()

def partTwo():
    y = input("\nDo you want to make another password? (y/n): ")
    if y == 'y' or 'yes':
        partOne()
    elif y == 'n' or 'no':
        sys.exit(0)
    else:
        print("\nYou …
Run Code Online (Sandbox Code Playgroud)

python function exit quit

0
推荐指数
1
解决办法
115
查看次数

python if ... elif总是不工作

很抱歉发布这样一个天真的问题,但我只是无法解决这个问题.我写了以下条件语句:

if taxon == "Bracelets":
    catId = "178785"
elif taxon == "Kids Earrings" or "Earrings":
    catId = "177591"
elif taxon == "Mangalsutras":
    catId = "177595"
elif taxon == "Necklaces" or "Necklace Sets":
    catId = "177597"
elif taxon == "Kids Pendants" or "Pendants":
    catId = "177592"
elif taxon == "Pendant Sets":
    catId = "177593"
elif taxon == "Anklets":
    catId = "178788"
elif taxon == "Toe Rings":
    catId = "178787"
elif taxon == "Rings":
    catId = "177590"
else:
    print "no match\n"
Run Code Online (Sandbox Code Playgroud)

但无论分类单位的价值是什么,它总是落在第二个条件下,即 …

python

0
推荐指数
2
解决办法
355
查看次数

遇到if/elif语句有问题

对于代码的"狗"部分,它可以完美地工作并完成它应该做的事情.但是,如果您在开始时为输入问题输入"Cat",它仍会继续并执行代码的狗部分.

即使我在代码中写道,如果问题的答案是=="Cat"或"cat",那么它应该执行此部分而不是Dog部分.

import time
import sys

animal=input("What animal do you want to calculate the age of? - Possible choices: Cat/Dog")

if animal=="Dog"or"dog":
    age=int(input("How old is your Dog?"))
    if age==1:
        print("Calculating the age of the Dog...")
        time.sleep(1)
        print("The age of the animal is: 11")
    elif age==2:
        print("Calculating the age of the Dog...")
        time.sleep(1)
        print("The age of the animal is: 11")
    else:
        age=age-2
        print("Calculating the age of the Dog...")
        time.sleep(1)
        agecalculation=age*4+22
        print("The age of the animal is:",agecalculation)
        time.sleep(2)
        print("End of program.")
        time.sleep(2) …
Run Code Online (Sandbox Code Playgroud)

python if-statement

0
推荐指数
1
解决办法
869
查看次数

当条件满足时,Python while循环不会中断

我只是想知道为什么循环在满足这些条件并且过滤到我的其他函数时不会中断?我通过做一个真正的循环来修复它,并且只是打破每个if语句,但我想知道这样做有什么问题.

def main_entrance():

print "\n\tYou are in the main entrance. It is a large room with" 
print "\ttwo doors, one to the left and one to the right. There"
print "\tis also a large windy stair case leading up to a second floor."
print "\n\tWhat are you going to do?\n"
print "\t #1 take the door on the left?"
print "\t #2 take the door on the right?"
print "\t #3 take the stairs to the second floor?"

choice = …
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
2
解决办法
895
查看次数

非常基本的python

得到错误,但我不知道为什么......(我正在学习)我的代码;

import random
input("Hit enter to roll the dice")
global answer
def rollDice():
    result = random.randrange(1,6)
    print ("It landed on.." + str(result))
    answer = input("would you like to play again? [y/n]")
rollDice();


if (answer == "y" or "Y"):
    rollDice();
Run Code Online (Sandbox Code Playgroud)

错误; (一些剧本的作品)

Hit enter to roll the dice
It landed on..5
would you like to play again? [y/n]y
Traceback (most recent call last):
  File "diceRoller.py", line 11, in <module>
    while (answer == "y" or "Y"):
NameError: name 'answer' is not defined
Run Code Online (Sandbox Code Playgroud)

python dice

0
推荐指数
1
解决办法
96
查看次数

类型错误:player_attack() 缺少 1 个必需的位置参数:'self'

我一直收到这个错误,我完全不知道为什么,我有 3 个文件相互导入信息,但是这个主要文件只从 Monster_01.py 及其 player.py 中提取信息。

TypeError: player_attack() missing 1 required positional argument: 'self'
Run Code Online (Sandbox Code Playgroud)

---主要代码---

'''Player class
Fall 2014
@author Myles Taft (mbt6)
'''

import random
import monster_01



class Player():

    def __init__(self, strength = 0, player_hp = 0):
        self.strength = 1
        self.player_hp = 100
        print(self.strength, self.player_hp)

    def battle():



        def player_attack(self):
            print('success')
            while self.enemy_hp > 0:
                monster_01()
                self.dice1 = random.randint(1,6)
                self.dice2 = random.randint(1,6)
                self.dice_sum = dice1 + dice2
                self.attack = dice1 + dice2
                self.decide_roll = input('Type "roll" to roll the …
Run Code Online (Sandbox Code Playgroud)

python windows python-3.x

0
推荐指数
1
解决办法
2411
查看次数

多个Python if-statments?

我通常不用Python开发,但我有一些IDE和代码编辑器,我经常使用和切换.为了让自己更方便,我想我会制作一个快速的Python程序,根据输入启动我的IDE/Editor.问题是,每次运行程序时,第一个if-statment总是验证为true并运行该操作.

这是我的代码:

import os

#NOTE: I have trimmed the root directories here to save space. Just removed the subfolder names, but the programs are the same. 

notepadPlusPlusLaunch = "C:\\Notepad++\\notepad++.exe"
bracketsLaunch = "C:Brackets\\Brackets.exe"
aptanaLaunch = "C:Aptana Studio\\AptanaStudio3.exe"
devCppLaunch = "C:Dev-Cpp\\devcpp.exe"
githubLaunch = "C:GitHub,  Inc\\GitHub.appref-ms"
androidLaunch = "C:android-studio\\bin\\studio64.exe"
ijLaunch = "C:bin\\idea.exe"
pycharmLaunch = "C:JetBrains\\PyCharm  4.0.5\\bin\\pycharm.exe"
sublimeLaunch = "C:Sublime Text 3\\sublime_text.exe"

def launcherFunction(command):
    os.startfile(command)

launchCommand = input("")

if launchCommand == "notepad" or "npp" or "n++" or "n":
    launcherFunction(notepadPlusPlusLaunch)

elif launchCommand == "brackets" or …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

0
推荐指数
1
解决办法
156
查看次数

Python:检查字符串是否等于其他两个字符串的更短方法

我刚刚开始学习python这个问题出现在我的脑海中,是否有更短的方法来确定一个字符串是否等于'某事'或'somethingelse'?

例:

input = raw_input("question?")

while input != 'n' and input != 'y':
    #ask question again
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
1
解决办法
117
查看次数