Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的程序,它将提示用户输入由7位数字组成的帐号?_Python_Python 3.x_Function - Fatal编程技术网

Python中的程序,它将提示用户输入由7位数字组成的帐号?

Python中的程序,它将提示用户输入由7位数字组成的帐号?,python,python-3.x,function,Python,Python 3.x,Function,我正在上一门Python入门课程,有点困在一项作业上。任何建议或资源将不胜感激 问题是: 用Python编写一个程序,提示用户输入由7位数字组成的帐号 从用户处获取该帐号后,验证该帐号是否有效。您应该有一个名为current_accts的列表,其中包含所有有效帐户 当前有效帐户如下所示,您必须在程序中使用它们 5679034 8232322 2134988 6541234 3984591 1298345 7849123 8723217 应在名为check_account()的函数中验证输入的帐号

我正在上一门Python入门课程,有点困在一项作业上。任何建议或资源将不胜感激

问题是: 用Python编写一个程序,提示用户输入由7位数字组成的帐号

从用户处获取该帐号后,验证该帐号是否有效。您应该有一个名为current_accts的列表,其中包含所有有效帐户

当前有效帐户如下所示,您必须在程序中使用它们

5679034 8232322 2134988 6541234 3984591 1298345 7849123 8723217

应在名为check_account()的函数中验证输入的帐号,该函数将接受用户输入的帐号并列出当前帐号。如果帐户有效,此函数应返回1;如果帐户无效,则返回0

这是我到目前为止所写的内容,但我被卡住了,并且在第6-15行收到缩进的语法错误。我还收到错误消息说变量“current_acts”没有定义

prompt = "Please, enter the 8 digit account  number: "
current_accts = current_accts[1:]
current_accts [-1] = "valid"


while True: 
    try:
      userinput = current_accts(prompt)
      if len(userinput ) > 8:
        raise ValueError()
      userinput = int(userinput)
    except ValueError:
     print('The value must be an 8 digit integer. Try again')
    else:
        break 

userinput = str(userinput)

a =int(userinput[7])+int(userinput[5])+int(userinput[3])+int(userinput[1])
b1 = str(int(userinput[6])*20)
b2 = str(int(userinput[4])*20)
b3 = str(int(userinput[2])*20)
b4 = str(int(userinput[0])*20)
y = int(b1[0])+int(b1[1])+int(b2[0])+int(b2[1])+int(b3[0])+int(b3[1])+int(b4[0])+int(b4[1])

x = (a+y)

if x % 10 == 0:
   print('The account number you entered is valid!')
else:
   print('The account number you entered is invalid!')
注意:如果您正在寻找伪造的代码,请不要阅读答案

我可以告诉你逻辑,我猜你做错了

  • 逻辑:
  • 检查账号是否为7位数字
  • 如果条件1为真,则检查它是否在给定帐户列表中

您可以通过检查is the condition
1000000来检查条件1。您确实需要回去学习python的一些基本语法,因为您的代码有很多问题。只是谷歌的python教程,或者谷歌特有的问题,比如如何在python中获得用户的输入

无论如何,我不是想侮辱你,只是想教书,所以这里是我解决你问题的方法(通常我不会解决所有问题,但很明显你做了一些努力):


我并没有像您的问题所要求的那样将其放在返回0或1的函数中,我建议您自己修改此代码以实现这一点

那么问题是什么?你有错误吗?你在哪里卡住了?我很难正确定义我的变量“current_accts”,也很难将分配给我的账号列表放在代码中的何处。
将账号列表放在何处
-将它们放在顶部的列表中,以便下面的所有内容都可以使用该列表它。
接收缩进语法错误
-修复缩进。
current_accts = ['5679034', '8232322', '2134988', '6541234', '3984591', '1298345', '7849123', '8723217']
user_input = input("Please, enter the 7 digit account  number: ")
if user_input in current_accts:
    print('The account number you entered is valid!')
else: 
    print('The account number you entered is invalid!')