无法在python中创建一个工作循环

时间:2015-02-03 05:20:37

标签: python loops

我对编程逻辑如此陌生我不确定我是否可以正确地说出这一点。我正在使用Python 2.7并且我正在尝试编写一个重复的脚本,直到输入为零。我已经尝试了if,else和while语句并得出结论,我对逻辑知之甚少,无法了解Python。例如......我很新, init 对我来说毫无意义。我已经在几乎所有我收到的搜索结果中都看到过这句话,但我不知道它的含义是什么。 I类是一个LOGIC类而不是Python类。我可以用伪代码写它但我真的很想看到一个工作模型。请帮忙。此脚本将运行并在输入零时退出,但不会提示再次驱动里程。

#Cost of Trip Ch2 Q8
print "To Calculate the cost of your trip,"
print "enter the miles driven or zero to quit"
getMiles = float(input ('Enter Miles: '))
while getMiles == 0:
    print "END OF PROGRAM"
    exit
fuelEcon = getMiles / 20
fuelCost = float(input ('Enter Cost of Fuel: $'))
costOfTrip = getMiles * fuelCost
fuelIncrease = (fuelCost * .1) + fuelCost
futureTrip = getMiles * fuelIncrease
while costOfTrip == float:
    getMiles
print "Cost of Trip: $", costOfTrip
print "Cost of Trip With 10% Increase in Fuel Cost: $", futureTrip

我忘记提及的是强制性的"结束计划"声明。我使用了你的答案的组合,这是有效的。再次,谢谢大家。我可以不停地把头撞在墙上。

#Cost of Trip Ch2 Q8
print "To Calculate the cost of your trip,"
print "enter the miles driven or enter zero to quit"
getMiles = float(raw_input ('Enter Miles: '))
while getMiles >= 0:
    if getMiles == 0:
        print "END OF PROGRAM"
        exit()
    fuelEcon = getMiles / 20
    fuelCost = float(input ('Enter Cost of Fuel: $'))
    costOfTrip = getMiles * fuelCost
    fuelIncrease = (fuelCost * .1) + fuelCost
    futureTrip = getMiles * fuelIncrease
    print "Cost of Trip: $", costOfTrip
    print "Cost of Trip With 10% Increase in Fuel Cost: $", futureTrip
    getMiles = float(raw_input ('Enter Miles: '))

5 个答案:

答案 0 :(得分:1)

Python与伪代码的距离并不是很远,而且这里的问题确实不是代码而是逻辑。

获得基本"循环直到输入零"你可以有以下逻辑:

miles = -1
while miles != 0:
    miles = float(raw_input ('Enter Miles: '))

至于您自己的代码,您似乎正在使用'而#39;当你的意思是'如果' 而在第二种情况下,你实际上只是命名一个什么都不做的变量(getMiles)

整个代码可能如下所示:

miles = float(raw_input ('Enter Miles: '))
while miles != 0:
    fuelEcon = miles / 20
    fuelCost = float(input ('Enter Cost of Fuel: $'))
    costOfTrip = miles * fuelCost
    fuelIncrease = (fuelCost * .1) + fuelCost
    futureTrip = miles * fuelIncrease

    print "Cost of Trip: $", costOfTrip
    print "Cost of Trip With 10% Increase in Fuel Cost: $", futureTrip

    miles = float(raw_input ('Enter Miles: '))

**无需使用"而真正的"正如其他人所说的那样,做起来并不是一件好事。

更高级的版本是提取可重复且独立于函数的逻辑部分

def trip_cost(miles):
    if(miles == 0):
        return False
    fuelEcon = miles / 20
    fuelCost = float(input ('Enter Cost of Fuel: $'))
    costOfTrip = miles * fuelCost
    fuelIncrease = (fuelCost * .1) + fuelCost
    futureTrip = miles * fuelIncrease
    print "Cost of Trip: $", costOfTrip
    print "Cost of Trip With 10% Increase in Fuel Cost: $", futureTrip

    return True

while trip_cost(float(raw_input ('Enter Miles: '))):
    pass

关于init是什么,这是一个更为高级的对象定位主题,你可能不应该担心

答案 1 :(得分:1)

我只是说风格,即使你的新循环条件/逻辑可以快速整理阅读

public double getDistance(string origin, string destination)
    {
        System.Threading.Thread.Sleep(1000);
        double distance = 0;
        string url = "http://maps.googleapis.com/maps/api/directions/json?origin=" + origin + "&destination=" + destination + "&sensor=false";
        string requesturl = url;

        string content = fileGetContents(requesturl);
        JObject o = JObject.Parse(content);
        try
        {
            distance = (int)o.SelectToken("routes[0].legs[0].distance.value");
            return distance;
        }
        catch
        {
            return 0;
        }
    }

    protected string fileGetContents(string fileName)
    {
        string sContents = string.Empty;
        string me = string.Empty;
        try
        {
            if (fileName.ToLower().IndexOf("http:") > -1)
            {
                System.Net.WebClient wc = new System.Net.WebClient();
                byte[] response = wc.DownloadData(fileName);
                sContents = System.Text.Encoding.ASCII.GetString(response);

            }
            else
            {
                System.IO.StreamReader sr = new System.IO.StreamReader(fileName);
                sContents = sr.ReadToEnd();
                sr.Close();
            }
        }
        catch { sContents = "unable to connect to server "; }
        return sContents;
    }

答案 2 :(得分:0)

你非常接近,这是固定版本:

print "To Calculate the cost of your trip,"
print "enter the miles driven or zero to quit"
while True:
    getMiles = float(input ('Enter Miles: '))
    if getMiles == 0:
        print "END OF PROGRAM"
        exit()
    fuelEcon = getMiles / 20
    fuelCost = float(input ('Enter Cost of Fuel: $'))
    costOfTrip = getMiles * fuelCost
    fuelIncrease = (fuelCost * .1) + fuelCost
    futureTrip = getMiles * fuelIncrease
    print "Cost of Trip: $", costOfTrip
    print "Cost of Trip With 10% Increase in Fuel Cost: $", futureTrip

我在整个代码块周围添加了while True,这将导致问题被反复询问(永远),直到用户输入0英里为止。

唯一需要解决的问题是exit是函数调用,因此它应该是exit()

答案 3 :(得分:0)

你可以这样做

#Cost of Trip Ch2 Q8
print "To Calculate the cost of your trip,"
print "enter the miles driven or zero to quit"

while True:
    getMiles = float(input ('Enter Miles: '))
    if getMiles == 0:
        print "END OF PROGRAM"
        break

    print 'Do the other calculations'

进入无限循环,直到输入0,此时你会退出循环并且程序结束。

你可以在python 2.7下使用While 1:来获得更快的性能,但我怀疑这是你现在关注的问题。

答案 4 :(得分:0)

total_miles = 0

打印"计算旅行费用,"

打印"输入行驶里程或零退出"

getMiles = float(输入('输入里程:'))

getMiles!= 0:

total_miles = getMiles + total_miles
getMiles = float(input ('Enter Miles: '))

否则:

print "END OF PROGRAM"
exit

fuelEcon = total_miles / 20

fuelCost = float(输入('输入燃料成本:$'))

costOfTrip = total_miles * fuelCost

fuelIncrease =(fuelCost * .1)+ fuelCost

futureTrip = total_miles * fuelIncrease

而costOfTrip == float:

getMiles

打印"旅行费用:$",costOfTrip

打印"燃料成本增加10%的旅行成本:$&#34 ;, futureTrip