【问题标题】:in range() statement doesnt work在 range() 语句中不起作用
【发布时间】:2018-08-11 03:34:14
【问题描述】:

`所以我只是在做一些东西来测试我是否可以根据输入做出某些输出

每次我尝试输入答案时,这里都没有任何反应 我使用了 if elif 语句,其中包含 in range() 。 当我输入答案时什么都没有显示

from random import randint
from termcolor import colored
repeat = True
from termcolor import colored
import time
import sys
import multiprocessing


   print colored("Hello, this is a program to help you practice algebraic 
    expressions.","magenta")
    print("")
     time.sleep(4)
     print colored("Alright let's start with something very basic...\nWhat is x 
     if 
     2x 
     = 32?","magenta")
     prob = int(raw_input())
   if int(prob) in range (16, 16):
     print colored("Well done, you got the warmup correct","yellow")
   elif int(prob) in range(1, 15):
     print colored("Incorrect answer try again","red")

【问题讨论】:

  • 我尝试格式化您的代码,但您的缩进已关闭,请修复它。
  • 您希望if int(prob) in range (16, 16): 做什么?因为它现在基本上什么都不做。
  • 您的代码仅涵盖 1 到 14 之间的输入。
  • 我在 (16, 16) 范围内做了,因为其他一切都失败了
  • 如果您检查range(16, 16) 返回的内容,那将是有意义的。

标签: python if-statement


【解决方案1】:

if int(prob) in range(16,16): 这一行有问题。函数range(x,y) 生成等于 [x,y) 的列表,这意味着以 x 开头并包括 x,以 y 结尾不包括 y,因此,您是在询问您的号码是否在空数组中。 例子: list_of_numbers = range(12,16) 为我们提供了包含 list_of_numbers = [12,13,14,15] 元素的列表

【讨论】:

    【解决方案2】:

    我编写了这段代码,删除了所有你没有使用过的函数和似乎总是出错的彩色文本。 range 通常用于“for”循环,但在条件句中,如果它等于 > 则需要使用 == 来表示大于

    import time
    print ("Hello, this is a program to help you practice algebraic 
    expressions.")
    print("")
    time.sleep(4)
    prob =int (input("Alright let's start with something very basic What is 
    x if 2x = 32? "))
    
    if prob ==16:
        print("Well done, you got the warmup correct")
    else:
        print("Incorrect answer try again")
               `
    

    【讨论】:

    • 不明白答案是什么问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 2016-04-25
    • 1970-01-01
    相关资源
    最近更新 更多