Home > Blockchain >  Code worked in coursera sandbox editor but fails in python 3
Code worked in coursera sandbox editor but fails in python 3

Time:07-01

I am super new to Python. I've been taking a Python Course on Coursera and fooling around with a little bit of code. Prior to today I was doing the class on a chromebook, and therefore using the sandbox tool provided. I've purchased an actual PC to download python since. I wrote a little code that was working just fine in the sandbox tool, but when I enter it into python it keeps booting me out. Can anyone spot anything obviously wrong with my code that I should change?

It should take an input of a date, exercise, and then keep looping through asking you about your sets and reps, saving results as tuples in a dictionary, until you enter 0, where it exits the loop, and prints the results. Stumped!

dt = input("Date:")
ex = input("Exercise:")
d = dict()
set = 0
while True:
    wrk = input("Insert as Reps x Weight: ")
    if wrk == "0":
        break
    set = set   1
    d[set] = wrk
   
print(dt)
print(ex)
print(d)

CodePudding user response:

Indentation is really important in Python since it uses whitespace to differentiate between blocks of code.

Here,

  1. observe the indentation of the while statement. Note that the indentation in the if block remains the same. This is because we want break to execute only if wrk is 0.
  2. On the other hand, we keep set 1 outside because the condition did not match and we wanted our program to keep running.
python
dt = input("Date:")
ex = input("Exercise:")
d = dict()
set = 0
while True:
    wrk = input("Insert as Reps x Weight: ")
    if wrk == "0":
        break
    set = set   1
    d[set] = wrk
   
print(dt)
print(ex)
print(d)

This works as expected.

CodePudding user response:

there were extra indents in the code now it will work properly.

  • Related