尝试:语句失败

问题描述:

我有失败一些Python代码:尝试:语句失败

import sys 
print ("MathCheats Times-Ed by jtl999") 
numbermodechoice = raw_input ("Are you using a number with a decimal? yes/no ") 
if numbermodechoice == "yes": 
    try: 
    numberx1 = float(raw_input('Enter first number: ')) 
except ValueError: 
    print ("Oops you typed it wrong") 
try: 
    numberx1 = float(raw_input('Enter first number: ')) 
except ValueError: 
    print ("Oops you typed it wrong") 
    numberx2 = (float)(raw_input('Enter second number: ')) 
elif numbermodechoice == "no": 
    print ("Rember only numbers are allowed") 
    numberx1 = (int)(raw_input('Enter first number: ')) 
    numberx2 = (int)(raw_input('Enter second number: ')) 
else: 
    print ("Oops you typed it wrong") 
    exit() 
print ("The answer was") 
print numberx1*numberx2 
ostype = sys.platform 
if ostype == 'win32': 
    raw_input ("Press enter to exit") 
elif ostype == 'win64': 
    raw_input ("Press enter to exit") 

(全码here

我想包装用try语句的浮点运算,所以如果一个ValueError发生,它被逮住。这里是输出:

 
File "./Timesed.py", line 23 
    try: 
    ^
IndentationError: expected an indented block 

它是什么问题,我该如何解决这个问题?

+1

@Senthil:如果你纠正中有问题的代码并没有什么帮助。 – 2011-02-12 02:06:38

+0

@ S.Lott因为这个新用户可能试图学习Python,并且可能犯了很多错误。 – 2011-02-12 02:07:22

+0

您需要为每一行提供确切的缩进。用8个空格替换所有制表符并复制确切的文件。您可以在脚本上运行python -tt [如果将文本保存为脚本],它会告诉您是否正确缩进 – 2011-02-12 02:08:53

Python是空白敏感的,关于领先的空白。

你的代码可能应缩进像

import sys 
from sys import exit 
print ("MathCheats Times-Ed by jtl999") 
numbermodechoice = raw_input ("Are you using a number with a decimal? yes/no ") 
if numbermodechoice == "yes": 
    try: 
     numberx1 = float(raw_input('Enter first number: ')) 
     numberx2 = float(raw_input('Enter second number: ')) 
    except ValueError: 
     print ("Oops you typed it wrong") 
     exit() 
elif numbermodechoice == "no": 
    print ("Remember only numbers are allowed") 
    try: 
     numberx1 = (int)(raw_input('Enter first number: ')) 
     numberx2 = (int)(raw_input('Enter second number: ')) 
    except ValueError: 
     print ("Oops you typed it wrong")   
     exit() 
else: 
    print ("Oops you typed it wrong") 
    exit() 
print ("The answer was") 
print numberx1*numberx2 
ostype = sys.platform 
if ostype == 'win32': 
    raw_input ("Press enter to exit") 
elif ostype == 'win64': 
    raw_input ("Press enter to exit") 

您的语法错误。它应该是except ValueError:而不是except: ValueError。在问题中也为你纠正它。

您需要缩进第二个print声明。

缩进在Python中很重要。这就是你用这种语言划分块的方式。

到float的转换使用的语法不正确。该语法适用于C/C++/Java,但不适用于Python。它应该是:

numberx1 = float(raw_input('Enter first number: ')) 

这将这样解释float("2.3"),这对float类型构造函数的字符串参数被调用。而且,对,函数调用的语法完全相同,所以您甚至可能认为构造函数是返回对象的函数。

在python中,代码的缩进非常重要。您向我们显示的错误点在这里:

if numbermodechoice == "yes": 
    try: 
    numberx1 = float(raw_input('Enter first number: ')) 
except ValueError: 
    print ("Oops you typed it wrong") 

作为块一部分的所有代码都必须缩进。通过启动try块,以下行是该块的一部分,并且必须缩进。修复它,缩进它!

if numbermodechoice == "yes": 
    try: 
     numberx1 = float(raw_input('Enter first number: ')) 
    except ValueError: 
     print ("Oops you typed it wrong") 

import sys 

class YesOrNo(object): 
    NO_VALUES = set(['n', 'no', 'f', 'fa', 'fal', 'fals', 'false', '0']) 
    YES_VALUES = set(['y', 'ye', 'yes', 't', 'tr', 'tru', 'true', '1']) 

    def __init__(self, val): 
     super(YesOrNo,self).__init__() 
     self.val = str(val).strip().lower() 

     if self.val in self.__class__.YES_VALUES: 
      self.val = True 
     elif val in self.__class__.NO_VALUES: 
      self.val = False 
     else: 
      raise ValueError('unrecognized YesOrNo value "{0}"'.format(self.val)) 

    def __int__(self): 
     return int(self.val) 

def typeGetter(dataType): 
    try: 
     inp = raw_input 
    except NameError: 
     inp = input 

    def getType(msg): 
     while True: 
      try: 
       return dataType(inp(msg)) 
      except ValueError: 
       pass 
    return getType 

getStr  = typeGetter(str) 
getInt  = typeGetter(int) 
getFloat = typeGetter(float) 
getYesOrNo = typeGetter(YesOrNo) 

def main(): 
    print("MathCheats Times-Ed by jtl999") 

    isFloat = getYesOrNo("Are you using a number with a decimal? (yes/no) ") 
    get = (getInt, getFloat)[int(isFloat)] 

    firstNum = get('Enter first number: ') 
    secondNum = get('Enter second number: ') 

    print("The answer is {0}".format(firstNum*secondNum)) 

if __name__=="__main__": 
    main() 
    if sys.platform in ('win32','win64'): 
     getStr('Press enter to exit')