我的循环缩进看起来很奇怪,这是正常的吗? (python 2)

问题描述 投票:0回答:3

我正在尝试在满足某些条件时创建循环,并且到目前为止它可以工作但是当我尝试创建更多条件时,我必须缩进它们才能使我的程序工作。

def terrain(surface):
    surface = raw_input("What surface will you be driving on? ")
    while surface != "ice" and surface != "concrete" and surface != "soil" and surface != "asphalt" :
        surface = raw_input("Surface not valid. Please enter a valid surface: ")
    if surface == "ice":
                u = raw_input("what is the velocity of the car in meters per second? ")
                u = int(u)
                while int(u) < 0:
                    u = raw_input("Velocity must be a positive integer: ")
                                while int(u) == 0:
                                    u = raw_input("Velocty must be a number greater than zero: ")
                        while int(u) > 72:
                                                                u = raw_input("This vehicle cannot reach this speed: ")
                a = raw_input("How quickly is the vehicle decelerating? ")
                a = int(a)
                while int(a) > 0:
                    a = raw_input("Deceleration cannot be a positive integer: ")
                else: 
                        s1 = u**2
                        s2 = 2*.08*9.8
                    s = s1/s2
                    print "This is how far the vehicle will travel on ice: "
                    print ("The vehicle will travel %i meters before coming to a complete stop" % (s))
terrain("ice")

我只是希望能够创建更多没有野生缩进的循环

python python-2.7 if-statement while-loop indentation
3个回答
0
投票

如果我错了,这就是我理解的评论: 1.获得表面价值 2.如果(表面无效)请求另一个值 3.如果(表面是冰): 一个。为你获得价值 湾得到一个价值 C。使用公式计算s s1 = u**2 s2 = 2*.08*9.8 s = s1/s2 d。显示s 4.如果表面不是冰,则不做任何事情(根据您发布的代码,但可以编辑为具有类似if surface == "soil"的块)如果是这样的话

def terrain():
    surface = raw_input("What surface will you be driving on? ")
    valid_surface = ["ice", "concrete", "soil", "asphalt"]
    while surface not in valid_surface:
        surface = raw_input("Surface not valid. Please enter a valid surface: ")
    if surface == "ice":
        u = raw_input("what is the velocity of the car in meters per second?")
        while int(u) < 0:
            u = raw_input("Velocity must be a positive integer: ")
        while int(u) == 0:
            u = raw_input("Velocty must be a number greater than zero: ")
        while int(u) > 72:
            u = raw_input("This vehicle cannot reach this speed: ")
        a = raw_input("How quickly is the vehicle decelerating? ")
        a = int(a)
        while int(a) > 0:
            a = raw_input("Deceleration cannot be a positive integer: ")
        while int(a) < -55: #note all while blocks are at same level
            #Do stuff  #contents inside while is indented by 4 spaces
            a = raw_input("This vehicle cannot have this deceleration. Please input another value: ")

        s1 = u**2
        s2 = 2*.08*9.8
        s = s1/s2
        print "This is how far the vehicle will travel on ice: "
        print ("The vehicle will travel %i meters before coming to a complete stop" % (s))

我通常定义一个函数:

def get_int(prompt, error):
    variable = input(prompt)
    while True:
        try:
            variable = int(variable) 
        except ValueError:
            variable = input(error)
            continue
        break
    return variable

并将其用作:

v1 = get_int("Enter value for accn: ", "Please input integer: ")
v2 = get_int("Enter value for velocity: ", "Please input integer: ")

如果需要接受浮动(例如1.25)值,您可能需要更改某些内容,例如将variable = int(variable)更改为variable = float(variable)


0
投票
  1. 每当你缩进时,你应该总是添加相同数量的缩进。在一个地方不需要使用4个缩进空间,在其他地方使用12个空格。最常见的惯例是使用4个空格。
  2. 仅在启动新块时添加缩进级别,即以冒号结尾的行后面的行。
  3. 结束块时,以下行必须与启动该块的关键字对齐。当然,如果多个块同时结束,则可能意味着超过一个级别。

这是您的代码可能看起来没有与缩进相关的语法错误:

def terrain(surface):
    surface = raw_input("What surface will you be driving on? ")
    while surface != "ice" and surface != "concrete" and surface != "soil" and surface != "asphalt" :
        surface = raw_input("Surface not valid. Please enter a valid surface: ")
    if surface == "ice":
        u = raw_input("what is the velocity of the car in meters per second? ")
        u = int(u)
        while int(u) < 0:
            u = raw_input("Velocity must be a positive integer: ")
            while int(u) == 0:
                u = raw_input("Velocty must be a number greater than zero: ")
            while int(u) > 72:
                u = raw_input("This vehicle cannot reach this speed: ")
        a = raw_input("How quickly is the vehicle decelerating? ")
        a = int(a)
        while int(a) > 0:
            a = raw_input("Deceleration cannot be a positive integer: ")
        else: 
            s1 = u**2
            s2 = 2*.08*9.8
            s = s1/s2
            print "This is how far the vehicle will travel on ice: "
            print ("The vehicle will travel %i meters before coming to a complete stop" % (s))
terrain("ice")

0
投票

您的代码存在很多问题,我将指出其中的一些问题。你也应该开始使用python3.x而不是python2你可以直接解析你从用户那里得到的输入int(raw_input("xyz")) 记住以上所有内容,下面的代码应该可行

import sys

def terrain():
    surface = raw_input("What surface will you be driving on? ")
    vel = -1
    while surface != "ice" and surface != "concrete" and surface != "soil" and surface != "asphalt" :
        surface = raw_input("Surface not valid. Please enter a valid surface: ")
    if surface == "ice":
        vel = int(raw_input("what is the velocity of the car in meters per second? "))
    while vel < 0:
        vel = int(raw_input("Velocity must be a positive integer: "))
    while vel == 0:
        vel = int(raw_input("Velocty must be a number greater than zero: "))
    while vel > 72:
        vel = int(raw_input("This vehicle cannot reach this speed: "))
    acc = int(raw_input("How quickly is the vehicle decelerating? "))
    while acc > 0:
        acc = int(raw_input("Deceleration cannot be a positive integer: "))

    s1 = vel**2
    s2 = 2*.08*9.8
    s = s1/s2
    print ("This is how far the vehicle will travel on ice: ")
    print ("The vehicle will travel %i meters before coming to a complete stop" % (s))

terrain()

阅读缩进如何在python中工作,因为它们是启动新代码块的方法,并尝试在我为您格式化的代码之上添加更多条件,然后您可以更好地理解它。

© www.soinside.com 2019 - 2024. All rights reserved.