简体   繁体   English

代码在 coursera 沙盒编辑器中有效,但在 python 3 中失败

[英]Code worked in coursera sandbox editor but fails in python 3

I am super new to Python. I've been taking a Python Course on Coursera and fooling around with a little bit of code.我是 Python 的超级新手。我一直在参加 Coursera 上的 Python 课程并学习一些代码。 Prior to today I was doing the class on a chromebook, and therefore using the sandbox tool provided.今天之前,我在 chromebook 上执行 class,因此使用提供的沙盒工具。 I've purchased an actual PC to download python since.从那以后,我购买了一台实际的 PC 来下载 python。 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.我写了一些在沙盒工具中运行良好的小代码,但是当我将它输入 python 时,它一直将我引导出去。 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.它应该输入日期、练习,然后不断循环询问你的组数和次数,将结果作为元组保存在字典中,直到你输入 0,然后退出循环并打印结果。 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)

Indentation is really important in Python since it uses whitespace to differentiate between blocks of code.缩进在 Python 中非常重要,因为它使用空格来区分代码块。

Here,这里,

  1. observe the indentation of the while statement.观察while语句的缩进。 Note that the indentation in the if block remains the same.请注意, if块中的缩进保持不变。 This is because we want break to execute only if wrk is 0.这是因为我们希望仅当wrk为 0 时才执行break
  2. On the other hand, we keep set+1 outside because the condition did not match and we wanted our program to keep running.另一方面,我们将set+1保留在外面,因为条件不匹配,我们希望我们的程序继续运行。
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.这按预期工作。

there were extra indents in the code now it will work properly.😁代码中有额外的缩进现在它可以正常工作了。😁

code:代码:

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 screenshot is to answer the @PeterFonteneau 's question.这个截图是为了回答@PeterFonteneau 的问题。 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM