简体   繁体   English

不会终止的Python程序

[英]Python program that wont terminate

I'm running the following code from the command line (python filename.py) and it wont terminate. 我从命令行(python filename.py)运行以下代码,它不会终止。 I've tried the code outside of a procedure and have tried the procedure in an online interpreter, so I don't think it's the the algorithm. 我已经在程序外部尝试过代码,并在在线解释器中尝试了该程序,所以我不认为这是算法。 What am I doing wrong? 我究竟做错了什么?

    n = raw_input("Enter a number: ")

    def print_multiplication_table(n):

        x = 1

        while x <= n:
            y = 1
            while y <= n:
                z = x * y
                print x, " * ", y, " = ", z
                y += 1
            x += 1

    print_multiplication_table(n)

You should convert the number received from raw_input into an integer. 您应该将从raw_input接收的数字转换为整数。 Right now it's being compared as a string. 现在,它正在作为字符串进行比较。

An easy (but probably bad) way to do this: 一种简单(但可能很糟糕)的方法:

n = int(raw_input("Enter a number: "))

There is a problem with the raw_input command. raw_input命令存在问题。 I have a similar code myself (guess we're both following the Udacity course). 我自己也有类似的代码(猜测我们都在遵循Udacity课程)。 I tried to add the raw_input line to my code, and it ended up in an infinite loop too. 我试图将raw_input行添加到我的代码中,并且它最终也陷入了无限循环。

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

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