Jump to content

Help - Whats wrong with my code?

- - - - -

  • Please log in to reply
2 replies to this topic

#1
pythonchallenge

pythonchallenge

    Newbie

  • Members
  • Pip
  • 3 posts
Question:

Write a program to monitor the price of shares in your latest technology start up.

Your program should read in a sequence of floats all on one line, and if the share price decreases without ever increasing in the sequence, your program should output Crashed!, like this:
Prices: 5.0 4.8 3.2 2.0
Crashed!
or this:
Prices: 1.0 0.7 0.7 0.7 0.1
Crashed!
If the share price remains constant, or if it goes up and down, your program should output Everything is OK.:
Prices: 3.0 4.5 2.0 1.0
Everything is OK.
and for this case too:
Prices: 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
Everything is OK.

This is what i have gotten so far what is wrong with it?

 price=raw_input("Prices:")

list=price.split()

i=0

test=1

a=1

octopus=5

while i <(len(list)-1):

    if float(list[i]) > float(list[i+1]):

        a=0

    elif float(list[i]) == float(list[i + 1]):

        octopus=1

    else:

        c = 1

        break

    i = i + 1


if a ==0:

    print "Crashed!"

else:

    print "Everything is OK."

The error i received was the following by a automatic tester:

Test cases:

testing the first example in the question: passed
testing the second example in the question: passed
testing the third example in the question: passed
testing the fourth example in the question: passed
testing a case with a strictly decreasing sequence: passed
testing a case with a strictly increasing sequence: passed
testing a case where the share price remains constant (and is over 9000!): passed
testing a case where the share price decreases and then increases: failed

Your submission output
Prices:10.0 8.5 8.6
Crashed!
when it was meant to output
Prices: 10.0 8.5 8.6
Everything is OK.

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Can you show your entire program, including the imports and such? Or is this it?

(And why did you name your variable octopus?)
sudo rm -rf /

#3
Revolt

Revolt

    Programmer

  • Members
  • PipPipPip
  • 99 posts
I think you meant to write a = 1 on the else clause.

If you notice a decrease you set a = 0. However, you then noticed an increase and set c = 1 and broke the for. Out of the for a is checked and since it is still == 0 you output crash




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users