我正在做一个排名类型的事情,发生的事情是我将得分与当前得分进行比较,如果得分低于当前,那么玩家获得了高分,但在此处使用此代码时
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 代码中得到未捕获的异常(错误),这些异常被描述为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程序.我跑了它打印yes,实际上我希望它不打印任何东西因为14不大于14.
我看到了这个相关的问题,但它没有多大帮助.
#! /usr/bin/python
import sys
hours = "14"
if (hours > 14):
print "yes"
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我不明白代码的问题是什么,它非常简单,所以这很简单.
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)
问题是什么?
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():来检查用户输入的内容是否是有效数字.
我是编码的新手,所以我决定使用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) 不幸的是raw_input没有做我需要它做的事情.我想要做的是获取totPrimes =我在提示符下键入的内容.如果我while count < totPrimes用while 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) 我只是在玩输入和变量.我正在尝试运行一个简单的函数:
slope = (y2-y1)/(x2-x1)
Run Code Online (Sandbox Code Playgroud)
我想提示用户输入y2,y1,x2和x1.什么是最简单,最干净的方法?
我是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) 对不起,如果这是一个可怕的问题,但我真的很喜欢编程.我正在尝试一个简短的小测试程序.
如果我输入任何小于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 ×10
if-statement ×2
comparison ×1
debugging ×1
input ×1
raw-input ×1
typeerror ×1
types ×1
unicode ×1
user-input ×1