【问题标题】:JES while loopsJES while 循环
【发布时间】:2015-11-20 11:00:39
【问题描述】:

我正在处理一项任务,但我一直遇到 while 循环问题。下面从图像上的第一个数字画一条线,然后到输入的下一个数字。它也循环这个,但它每次都要求第一个和第二个数字,而不是去分机和下一个等。

谁能帮我解决这个问题?

def level1():
    cityXvalue = [45,95,182,207,256,312,328,350,374,400]
    cityYvalue = [310,147,84,201,337,375,434,348,335,265]

    # Display the map image
    map = makePicture("C:/Users/glarm/OneDrive/Documents/RMIT/Introduction to programming/Assignments/Assingment 2/map.png")
    show(map)
    numCities=requestInteger("Enter the number of cities you wish to visit:")
    counter=0
    # Draw a line between previously entered point and current one
    while numCities > 0:
     currentCity = requestInteger("Enter the city you would like to visit")
     previousCity = requestInteger("Enter the city you would like to visit next")
     #currently asking for first city then the next but you need to enter the previous city before the next... 
     startX = currentCity
     startY = currentCity
     endX = previousCity 
     endY = previousCity 
     addLine(map, cityXvalue[startX], cityYvalue[startY], cityXvalue[endX], cityYvalue[endY])
     counter=counter+1  
     repaint(map) 

【问题讨论】:

    标签: python loops while-loop jes


    【解决方案1】:

    我想通了,这是代码……

     def maplevel1():
        cityXvalue = [45,95,182,207,256,312,328,350,374,400]
        cityYvalue = [310,147,84,201,337,375,434,348,335,265]
    
        # Display the map image
        map = makePicture("C:/Users/glarm/OneDrive/Documents/RMIT/Introduction to programming/Assignments/Assingment 2/map.png")
        show(map)
        numCities=requestInteger("Enter the number of cities you wish to visit:")
        counter=0
        # Draw a line between previously entered point and current one
        while numCities > 0:
         startCity = requestInteger("Enter the city you would like to visit")
         nextCity = requestInteger("Enter the city you would like to visit next")
    
         startX = startCity
         startY = startCity
         endX = nextCity 
         endY = nextCity 
         addLine(map, cityXvalue[startX], cityYvalue[startY], cityXvalue[endX], cityYvalue[endY])
         counter=counter+1
         repaint(map)
    
         while counter>0:
          startCity=nextCity
          nextCity=requestInteger("Enter the city you would like to visit next")
          startX = startCity
          startY = startCity
          endX = nextCity 
          endY = nextCity 
          addLine(map, cityXvalue[startX], cityYvalue[startY], cityXvalue[endX], cityYvalue[endY])
          repaint(map)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-02
      • 2016-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多