-2

Trying to make a program that provides the price to different numbers of stages. In "tripss.txt",third line is the number 12, python interprets it as 1 and 2 instead and gives the price for each rather than the number as a whole, any way to fix it to that it's read as 12?.

infile = open("tripss.txt","r")  
customer_one= infile.readline().strip("\n")     
customer_two= infile.readline().strip("\n")  
customer_three= infile.readline().strip("\n")      
one_to_three_stages="euro 1.55"   
four_to_seven_stages="euro 1.85"   
seven_to_eleven_stages="euro 2.45"  
more_than_eleven_stages="euro 2.85"  
cheapest = ["1","2","3"]       
cheap = ["4","5","6","7"]    
expensive = ["7","8","9","10","11"]     
    for number in customer_three:     
    if number in cheapest:   
        print one_to_three_stages    
    elif number in cheap:     
        print four_to_seven_stages    
    elif number in expensive:    
        print seven_to_eleven_stages   
    else:         
        print more_than_eleven_stages      
Vik
  • 1
  • 1

2 Answers2

0

In your code it seems that you want to consider customer_three as a list of strings. However in your code it is a string, not a list of strings, and so the for loop iterates on the characters of the string ("1" and "2").
So I suggest you to replace:

customer_three= infile.readline().strip("\n")  

with:

customer_three= infile.readline().strip("\n").split()
Laurent H.
  • 6,316
  • 1
  • 18
  • 40
0

You say third line is the number 12 so after customer_three= infile.readline().strip("\n"), customer_three will be the string "12". If you then start the for loop for number in customer_three:, number will be assigned each element of the string - first "1", and then "2".

The solution is simple. customer_three already has the string you want. Remove the for loop completely.

tdelaney
  • 73,364
  • 6
  • 83
  • 116