相关疑难解决方法(0)

大于小于,python

我正在做一个排名类型的事情,发生的事情是我将得分与当前得分进行比较,如果得分低于当前,那么玩家获得了高分,但在此处使用此代码时

        print "Score = " + str(score) + ", Compared to = " + str(array[x])
        if score < array[x]:
                #Do stuff here
Run Code Online (Sandbox Code Playgroud)

但即使得分为4且数组[x]为2,if语句仍然完成?

难道我做错了什么?

我的理解是,如果得分4和数组[x]是2,则4大于2,这意味着它返回False?


下面是完整的代码

def getRank(array, score):
    rank = 0
    rankSet = False
    for x in range(0, len(array)):
        print "Score = " + str(score) + ", Compared to = " + str(array[x])
        if score < array[x]:
            if not rankSet:
                rank = x
                print "Set rank to: " + str(rank)
                rankSet = True
        elif score == array[x] or score …
Run Code Online (Sandbox Code Playgroud)

python if-statement

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

我收到类型错误。我如何解决它?

我经常从我的 Python 代码中得到未捕获的异常(错误),这些异常被描述为TypeErrors. 经过大量的实验和研究,我收集了以下示例(以及细微的变化):

TypeError: func() takes 0 positional arguments but 1 was given
TypeError: func() takes from 1 to 2 positional arguments but 3 were given
TypeError: func() got an unexpected keyword argument 'arg'
TypeError: func() missing 1 required positional argument: 'arg'
TypeError: func() missing 1 required keyword-only argument: 'arg'
TypeError: func() got multiple values for argument 'arg'
TypeError: MyClass() takes no arguments
TypeError: unsupported operand type(s) for +: 'int' and 'str'
TypeError: can only concatenate str …
Run Code Online (Sandbox Code Playgroud)

python debugging typeerror

11
推荐指数
2
解决办法
8049
查看次数

如何在python中比较字符串和整数?

我有这个简单的python程序.我跑了它打印yes,实际上我希望它不打印任何东西因为14不大于14.

我看到了这个相关的问题,但它没有多大帮助.

#! /usr/bin/python

import sys

hours = "14"

if (hours > 14):
        print "yes"
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

python comparison

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

TypeError:'str'对象不能解释为整数

我不明白代码的问题是什么,它非常简单,所以这很简单.

x = input("Give starting number: ")
y = input("Give ending number: ")

for i in range(x,y):
 print(i)
Run Code Online (Sandbox Code Playgroud)

它给了我一个错误

Traceback (most recent call last):
  File "C:/Python33/harj4.py", line 6, in <module>
    for i in range(x,y):
TypeError: 'str' object cannot be interpreted as an integer
Run Code Online (Sandbox Code Playgroud)

例如,如果x为3且y为14,我希望它打印

Give starting number: 4
Give ending number: 13
4
5
6
7
8
9
10
11
12
13
Run Code Online (Sandbox Code Playgroud)

问题是什么?

python types

9
推荐指数
3
解决办法
9万
查看次数

AttributeError:'int'对象没有属性'isdigit'

numOfYears = 0
cpi = eval(input("Enter the CPI for July 2015: "))
if cpi.isdigit():
    while cpi < (cpi * 2):
        cpi *= 1.025
        numOfYears += 1
    print("Consumer prices will double in " + str(numOfYears) + " years.")
while not cpi.isdigit():
    print("Bad input")
    cpi = input("Enter the CPI for July 2015: ")
Run Code Online (Sandbox Code Playgroud)

我收到以下错误.

AttributeError:'int'对象没有属性'isdigit'

由于我是编程新手,我真的不知道它想告诉我什么.我正在使用它if cpi.isdigit():来检查用户输入的内容是否是有效数字.

python

8
推荐指数
3
解决办法
3万
查看次数

编码新手 - Python Error = TypeError:只能将str(不是"int")连接到str

我是编码的新手,所以我决定使用unicode为测试目的制作某种密码,我已经通过在Unicode中添加数字来做到这一点,所以这有点秘密.我一直在收到这个错误,但我不知道如何解决它.有什么解决方案吗?这是代码:

while True:
    try:
        message = int(input("Enter a message you want to be decrypt: "))
        break
    except ValueError:
        print("Error, it must be an integer")
secret_string = ""
for char in message:
    secret_string += chr(ord(char - str(742146))                      
print("Decrypted", secret_string)
q = input("")
Run Code Online (Sandbox Code Playgroud)

python unicode

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

Python:raw_input读取数字的问题

不幸的是raw_input没有做我需要它做的事情.我想要做的是获取totPrimes =我在提示符下键入的内容.如果我while count < totPrimeswhile count < 50这个脚本替换工作.如果我在提示符下键入50,这个脚本不起作用,我担心raw_input不是我想要使用的函数吗?这是我的代码片段:

testNum = 3
div = 2
count = 1
totPrimes = raw_input("Please enter the primes: ")

while count < totPrimes :
    while div <= testNum :
Run Code Online (Sandbox Code Playgroud)

python raw-input

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

如何在python中进行简单的用户输入?

我只是在玩输入和变量.我正在尝试运行一个简单的函数:

slope = (y2-y1)/(x2-x1)
Run Code Online (Sandbox Code Playgroud)

我想提示用户输入y2,y1,x2x1.什么是最简单,最干净的方法?

python user-input input

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

/:'str'和'int'不支持的操作数类型

我是Python新手,正在学习一些基础知识.我想知道为什么我会收到这个错误.代码是:

Hours = raw_input ("How many Hours you worked for today : ")
minutes = (Hours * 60)
Percentage = (minutes * 100) / 60
print "Today you worked : ", "percentage"
Run Code Online (Sandbox Code Playgroud)

python

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

比较数字会在Python中产生错误的结果

对不起,如果这是一个可怕的问题,但我真的很喜欢编程.我正在尝试一个简短的小测试程序.

如果我输入任何小于24的值,它会打印出"你将老了......"的声明.如果我输入任何大于24的值(仅限最多99),则会打印"你是旧的"语句.

问题是如果你输入一个100或更大的值,它会打印出"在你知道之前你会老去".声明.

print ('What is your name?')
myName = input ()
print ('Hello, ' + myName)
print ('How old are you?, ' + myName)
myAge = input ()
if myAge > ('24'):
     print('You are old, ' + myName)
else:
     print('You will be old before you know it.')
Run Code Online (Sandbox Code Playgroud)

python if-statement

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