相关疑难解决方法(0)

TypeError:+ =的不支持的操作数类型:'builtin_function_or_method'和'int'

TypeError: unsupported operand type(s) for +=: 'builtin_function_or_method' and 'int'在尝试运行此代码时收到此错误()

total_exams = 0
for total_exams in range(1, 100001):
    sum += total_exams
print(sum)

sum = 0
total_exams = 0
while count <= 100000:
    sum += total_exams
    total_exams += 1
print(sum)

sum = int("Please enter Exam grade, or press 999 to end: ")
while true:
    if sum <= 100:
        sum += total_exams
        total_exams += 1
    elif sum == "999":
        print(sum / total_exams)
Run Code Online (Sandbox Code Playgroud)

总之,我只需要运行程序直到输入999,然后找到输入的所有数字的平均值.至少一点帮助会很好.

所以我编辑了我的代码(新)

totalExams = 0
total_sum = 0
for …
Run Code Online (Sandbox Code Playgroud)

python

1
推荐指数
1
解决办法
4万
查看次数

如果不工作

我正在尝试创建一个简单的程序,我要求用户放置name,age然后使用if else打印结果.

我希望什么时候放到20岁,所以结果应该来

祝贺你的年龄是20,你可以找到真相

但这里不起作用的是我正在使用的代码.

name = input("what is your name \n")
print('Nice to meet you', name, "!!")
age = input("Your Age ? \n")

if age > 18:
    print('Congratulation ' + name + ' your age is '+ str(age), ' and you can find the truth ')
else:
    print('Sorry ' + name + 'your age is '+ str(age), ' and you can also find the truth if you start from today')
Run Code Online (Sandbox Code Playgroud)

运行我的代码后我得到的错误.

错误

what is …
Run Code Online (Sandbox Code Playgroud)

python

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

为什么if和elif语句没有完全执行?

在阅读2.7文档的基础上,以下代码应该从我能说的内容中正确地逐步完成.我调用raw_input(text)来获取变量年龄的输入,但它总是返回你可以看到评级为R的电影.

import sys

age = raw_input("What is your age? ")

if age >= 17:
    print "You can see rated R movies."

elif age < 17 and age > 12:
    print "You can see a rated PG-13 movies."

else:
    print "You can only see PG movies!"
Run Code Online (Sandbox Code Playgroud)

根据我的逻辑,如果传递给age的值不大于或等于17,它应该移动到下一个语句.这不是我得到的结果.它总是返回额定R线.例如,我输入3,它仍然给我额定的R响应.如果我将>翻转到<然后它总是返回"你只能看到PG电影!" 线.

思考?

python

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

类型错误:“str”和“int”的实例之间不支持“&gt;”,但我将字符串设为 Interger

我有一个我似乎无法修复的非常令人沮丧的错误,它告诉我字符串和整数不支持“<”,但我确保我使用 int() 函数将它们转换为整数

import random

print("Hi what is your name?")

name = input()

print("And your age please?")

age = input()

print(f"Now {name} pick a number that is lower than your age ({age}) but bigger than zero")

number_pick = input()

if number_pick != "":
    int(age)
    int(number_pick)
    if number_pick > 0:
        print(f"Good choice now the result of your {age} timesed by {number_pick} is..")
        print(age * number_pick)
    else:
        print("your number doesnt follow the requirements")
else:
    print("Please write your number pick")


Run Code Online (Sandbox Code Playgroud)

我失去了想法,我应该改变什么?

python python-3.x

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

Python 3.4.2循环没有

请求用户输入,当他们输入字母时,会出现错误消息(如我所愿),但即使用户输入数字,也会出现错误.如果输入正确的数据,我希望它从这个循环出来,然后到下一个名称.

python list

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

Python noob--可能是一个简单的拼写错误?

我真的无法理解为什么这段代码不起作用......这可能只是一个错字.但即便是我经验丰富的朋友也难过.提示只是"编写一个程序,告诉你给定列表中有多少4个." 它的全部工作除了计数表示零,无论我提交多少4.

def num_four(number_list): 
    count = 0

    print number_list
    for i in number_list:
        print i
        if i == 4:  
            count = count + 1
    return count
number_list = raw_input("Enter list of integers here: ")

print num_four(number_list)
Run Code Online (Sandbox Code Playgroud)

输出看起来像这样:

Enter list of integers here: 123444
123444
1
2
3
4
4
4
0
Run Code Online (Sandbox Code Playgroud)

python

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

**或pow()的不支持的操作数类型:Python3的'str'和'int'

我的Python3代码

def ask():

    while True:
        try:
            n = input('Input an integer: ')
        except:
            print ('An error occurred! Please try again!')
            continue
        else:
            break


    print ('Thank you, you number squared is: ', n**2)
Run Code Online (Sandbox Code Playgroud)

如果我想取一个数字的正方形,为什么会出错?

unsupported operand type(s) for ** or pow(): 'str' and 'int' 
Run Code Online (Sandbox Code Playgroud)

从命令行没问题

>>> 3**2
9
Run Code Online (Sandbox Code Playgroud)

python

-1
推荐指数
1
解决办法
1万
查看次数

在python IDE中返回连接而不是添加

x = input("Enter the first number!")
y = input("Enter the second number!")
z = input("Enter the third number!")

def adding(a, b, c):
    s = a+b+c
    return s

c = adding(x, y, z)
print(c)
Run Code Online (Sandbox Code Playgroud)

我在python IDE中尝试了一个添加程序,但它不是打印sum而是将数字串联作为字符串.

我不知道它有什么问题?

有人有什么想法吗?

抱歉! 对于一个愚蠢的问题.....

python

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

检查输入是否在python中的数字列表中

numbers = [1,2,3,4,5,6,7]
x = input()
if x in numbers:
    print("Hey you did it")
else:
    print("Nope")
Run Code Online (Sandbox Code Playgroud)

我不确定我在这里做错了什么,但它总是告诉我我的号码不在列表中......即使它是.但是,使用字符串工作正常.

帮助将不胜感激.谢谢!

python list python-3.x python-3.5

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

一起打印 string 和 int

让我们以这段代码为例:

Women = (input("What's the number of women?"))

Men = (input("What's the number of men?"))

print("Percentage of Men: " + ((Men//(Men+Women))*100) + "\n Percentage of Women: " + ((Women//(Men+Women))*100))
Run Code Online (Sandbox Code Playgroud)

我收到一个错误Class 'str' does not define '__floordiv__', so the '//' operator cannot be used on its instances

我该如何解决这个问题?

python

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

更多我的Python代码不会做它应该做的事情,出了什么问题?

money = 170
KitKat = 90
choice1 = raw_input("Choose a snack: ")
Run Code Online (Sandbox Code Playgroud)

选择一种小吃:KitKat

if choice1 < money:
    print ("Enjoy your snack")
else:
    print ("You cannot afford ") + (choice1)
Run Code Online (Sandbox Code Playgroud)

你买不起KitKat

这是我正在研究的自动售货机程序的缩短版本.你有一定数量的硬币,它要求你选择一个小吃.在这个缩短的程序中,你总是有170个硬币,你只能选择KitKat.然后通过比较变量来检查你是否可以买得起KitKat:"money"和"choice1"来看看哪个更大.如果"钱"更大,它应该说"享受你的零食".但如果"choice1"更大,它应该说"你买不起KitKat".

问题是,当我测试它时,它总是认为"choice1"大于"money"并且告诉我"我买不起KitKat".

我想也许当我输入"KitKat"时它不会将它识别为变量并将"KitKat"一词与变量"money"进行比较,而不是将变量"KitKat"与变量"money"进行比较但是我我不确定.我出错的任何想法?

python variables python-2.7

-4
推荐指数
1
解决办法
112
查看次数

我的if语句/调用问题有问题,请帮助:)

我的代码有一些问题,看看main()部分,我输入选择2,它调用open_file.但它需要1作为别的而不是IF ...而当我用1 agin写它只是在屏幕上打印1,我做错了什么?Python版本3.4.2.

import sys
name=input("What is your name? : ")
print ("Welcome " + name)


def main():
    print ("What do you want to do")
    print ("1) Open A File")
    print ("2) open Web Browser")
    print ("3) Exit OS")

    main_choice=input()

    if main_choice == 1:
        open_file()
    elif main_choice == 2:
        web_browser()
    elif main_choice == 3:
        exit_os()
    else:
        unknown_number()


def unknown_number():
    print ("The choice you made does not exist, please choose a valid option")
    main


def open_file():
    print ("What do you want …
Run Code Online (Sandbox Code Playgroud)

python

-4
推荐指数
1
解决办法
65
查看次数

小于操作员未正确操作

小于运算符对我来说不正常:

port = raw_input("Enter Port: ")
        if port <= 65535:
            print("Valid Port")
        else:
            print("Port Invalid")
Run Code Online (Sandbox Code Playgroud)

但是,每当我输入66到99之间的任何十或百,例如166或766或889的端口时,它将无效,但下面的任何内容都可以.它就像是只注册/看到"65"部分.我试过没有""65535左右,但总是返回无效.

好吧,让它排序,我不知道它作为一个字符串返回.虽然,我有点失望,因为我被撕裂了,我没有达到所有具有多年经验的人的"高额"标准.

python

-8
推荐指数
1
解决办法
629
查看次数

标签 统计

python ×13

list ×2

python-3.x ×2

python-2.7 ×1

python-3.5 ×1

variables ×1