首页 > 解决方案 > 我的项目不断让我退出,为什么会这样?(关闭)

问题描述

我正在编写一个代码,允许我将数据保存到数据库中。但是由于某种原因,每当我输入响应时,它都会引用 else 语句并将我注销。

这是我的代码:

username = "storagei"
password = "something"

# asking for a username and password.
userInput = input("welcome back to safespace! Please type in your username to access your data.\n")


if userInput == username:
    userInput = input("Password?\n")   
    if userInput == password:
       print("Welcome back to safespace Izzy!")
        
    else:
       print("That is the wrong password. Please try again.")
       exit(1)
else:
    print("There is no username that matches what your responce. Please try again.")
    exit(2)

print "You can open_storage, add_to_storage, or log_out"   
userInput = input("What would you like to do?\n")

log_out = "Thanks for using safespace. Come back soon!"
open_storage = "opening files."
add_to_storage = "what do you like to add?"

if userInput == log_out:
   print("Thanks for using safespace. Come back soon!")
   exit(3)
   if userInput == open_storage:
      print("opening storage. Please wait...")
   if userInput == add_to_storage:
      print("what would you like to add?")
else:
    print("not a valid input. Logging you out.")

所以出于某种原因,当它到达这部分代码时:


print "You can open_storage, add_to_storage, or log_out"   
userInput = input("What would you like to do?\n")

log_out = "Thanks for using safespace. Come back soon!"
open_storage = "opening files."
add_to_storage = "what do you like to add?"

if userInput == log_out:
   print("Thanks for using safespace. Come back soon!")
   exit(3)
   if userInput == open_storage:
      print("opening storage. Please wait...")
   if userInput == add_to_storage:
      print("what would you like to add?")
else:
    print("not a valid input. Logging you out.")

我自己修复了我的代码。如果您想查看它们,这是我拥有的所有编辑的工作脚本。

username = "storagei"
password = "something"

# asking for a username and password.
userInput = input("welcome back to safespace! Please type in your username to access your data.\n")


if userInput == username:
    userInput = input("Password?\n")   
    if userInput == password:
       print("Welcome back to safespace Izzy!")
        
    else:
       print("That is the wrong password. Please try again.")
       exit(1)
else:
    print("There is no username that matches what your responce. Please try again.")
    exit(2)

print "You can open_storage type in o. add_to_storage type in a. log_out type in l. delete_files type in d. or edit_files type in e."   
userInput = input("What would you like to do?\n")

l = "l"
o = "o"
a = "a"
d = "d"
e = "e"

if userInput == l:
   print("Thanks for using safespace. Come back soon!")
   exit(3)
if userInput == o:
      print("opening storage. Please wait...")
if userInput == a:
      print("what would you like to add?")
if userInput == d:
      print("what file do you want to delete?")
if userInput == e:
      print("what file do you want to edit?")
    
else:
  if userInput != l:
    if userInput != o:
      if userInput != a:
        if userInput != d:
          if userInput != e:
             print("not a valid input. Logging you out.")

标签: python

解决方案


尝试这个

if userInput == 'open_storage':
   print("opening storage. Please wait...")
elif userInput == 'add_to_storage':
   print("what would you like to add?")
elif userInput == 'log_out':
   print("Thanks for using safespace. Come back soon!")
   exit(3)
else:
   print("not a valid input. Logging you out.")

推荐阅读