如何编辑变量的值以供以后使用?(How to edit a variable's value for later use? [duplicate])
这个问题在这里已有答案:
- 在Python 10答案中 使用“global”关键字
所以我正在设置一个密码管理员以获得乐趣(不打算用于除自学之外的其他任何事情),我希望用户能够编辑他们的用户名和密码。
我设置它的方式是它要求你输入用户名和密码,它会根据几个非常具体的值检查你的响应(第74行),如果语句为真,它将允许访问,如果不是,它会拒绝访问。
然后我尝试设置语句“if(yourUser == yourNewUser and yourPass == yourNewPass):”当我尝试登录时,它给了我一个变量未定义的错误。
所以我尝试在早期定义变量,将每个变量设置为0,这样如果密码不变,它将转到常规位,如果它们已被更改或者如果它们不等于0,它将转到不同的功能做同样的事情,它有点工作,但后来拒绝我访问...
我不知道下一步该尝试什么。 如果您需要对我说的任何内容进行任何澄清,请询问,我会尽力提供帮助。
这是我的代码:
#The goodbye or end statement def goodbye(): print('Good Bye! :{D') #The main password request statement def request(): reqPass = input('Which password would you like?[Google, Twitter, Reddit, Computer]') #still need to figure out dictionary manipulation so this is a temporary sytem. if(reqPass == 'Google' or reqPass == 'google'): print('________________') print('Pass: GOOGLEPASSHERE') print('________________') reqSecPass = input('Request another password?[y/n]') if(reqSecPass == 'y' or reqSecPass == 'Y'): request() else: goodbye() elif(reqPass == 'twitter' or reqPass == 'Twitter'): print('_________________') print('User: TWITTERUSERHERE') print('Pass: TWITTERPASSHERE') print('________________') reqSecPass = input('Request another password?[y/n]') if(reqSecPass == 'y' or reqSecPass == 'Y'): request() else: goodbye() elif(reqPass == 'computer' or reqPass == 'Computer'): print('________________') print('Pass: COMPUTERPASSHERE') print('________________') reqSecPass = input('Request another password?[y/n]') if(reqSecPass == 'y' or reqSecPass == 'Y'): request() else: goodbye() elif(reqPass == 'reddit' or reqPass == 'Reddit'): print('_________________________') print('User: REDDITUSERHERE') print('Pass: REDDITPASSHERE') print('________________') reqSecPass = input('Request another password?[y/n]') if(reqSecPass == 'y' or reqSecPass == 'Y'): request() else: goodbye() #This is the start of the changed password function def changedpass(): if(yourUser == yourNewUser and yourPass == yourNewPass): dirCheck = input('Account settings?[y,n]') if(dirCheck == 'y' or dirCheck == 'Y'): #This is the start of the password/username thing print('this function is not working yet!') actSetCheck = input('Change username or password?') if(actSetCheck == 'user' or actSetCheck == 'User' or actSetCheck == 'Username' or actSetCheck == 'username'): yourNewUser = input('What would you like your new username to be?') elif(actSetCheck == 'pass' or actSetCheck == 'Pass' or actSetCheck == 'password' or actSetCheck == 'Password'): yourNewPass = input('What would you like your new password to be?') elif(dirCheck == 'n' or dirCheck == 'N'): request() #setting the variables early on yourNewUser == 0 yourNewPass == 0 #This is the "title screen" print('_____This is a password keeper_____') #checking if the user has an account actCheck = input('Do you already have an account?') if(actCheck == 'Yes' or actCheck == 'yes'): #Checking to see if the yourNewUser or yourNewPass variable has been changed if(yourNewUser != '0' or yourNewPass != '0'): changedpass() #asking for user's name and password else: yourUser = input('___What is your Username?___') yourPass = input('___What is your Password?___') if(yourUser == 'ari' and yourPass == 'rycbar1234'): dirCheck = input('Account settings?[y,n]') if(dirCheck == 'y' or dirCheck == 'Y'): #This is the start of the change password/username thing print('this function is not working yet!') actSetCheck = input('Change username or password?') if(actSetCheck == 'user' or actSetCheck == 'User' or actSetCheck == 'Username' or actSetCheck == 'username'): yourNewUser = input('What would you like your new username to be?') elif(actSetCheck == 'pass' or actSetCheck == 'Pass' or actSetCheck == 'password' or actSetCheck == 'Password'): yourNewPass = input('What would you like your new password to be?') elif(dirCheck == 'n' or dirCheck == 'N'): request() #incorrect password thing else: print('Incorrect Username or password')
This question already has an answer here:
- Use of “global” keyword in Python 11 answers
So i'm making a password keeper for fun(not intended to be used for anything other than teaching myself), and I want the user to be able to edit their username and password.
The way I had it set up is it asks you for a username and password, it checks your responses against a couple of very specific values(line 74), if the statement was true, it would allow access and if it isn't, it would deny access.
I then tried to set the statement "if(yourUser == yourNewUser and yourPass == yourNewPass):" it gives me a variable undefined error when I try to log in which I sort of expected.
So I tried defining the variables early on, setting each of them as 0 so if the password is unchanged, it would go to the regular bit, and if they have been changed or if they don't equal 0, it would go to a different function that does the same thing and it sort of worked, but then denied me access...
I don't know what to try next. If you need any clarification on anything that I said, just ask and I'll try to help.
Here's my code:
#The goodbye or end statement def goodbye(): print('Good Bye! :{D') #The main password request statement def request(): reqPass = input('Which password would you like?[Google, Twitter, Reddit, Computer]') #still need to figure out dictionary manipulation so this is a temporary sytem. if(reqPass == 'Google' or reqPass == 'google'): print('________________') print('Pass: GOOGLEPASSHERE') print('________________') reqSecPass = input('Request another password?[y/n]') if(reqSecPass == 'y' or reqSecPass == 'Y'): request() else: goodbye() elif(reqPass == 'twitter' or reqPass == 'Twitter'): print('_________________') print('User: TWITTERUSERHERE') print('Pass: TWITTERPASSHERE') print('________________') reqSecPass = input('Request another password?[y/n]') if(reqSecPass == 'y' or reqSecPass == 'Y'): request() else: goodbye() elif(reqPass == 'computer' or reqPass == 'Computer'): print('________________') print('Pass: COMPUTERPASSHERE') print('________________') reqSecPass = input('Request another password?[y/n]') if(reqSecPass == 'y' or reqSecPass == 'Y'): request() else: goodbye() elif(reqPass == 'reddit' or reqPass == 'Reddit'): print('_________________________') print('User: REDDITUSERHERE') print('Pass: REDDITPASSHERE') print('________________') reqSecPass = input('Request another password?[y/n]') if(reqSecPass == 'y' or reqSecPass == 'Y'): request() else: goodbye() #This is the start of the changed password function def changedpass(): if(yourUser == yourNewUser and yourPass == yourNewPass): dirCheck = input('Account settings?[y,n]') if(dirCheck == 'y' or dirCheck == 'Y'): #This is the start of the password/username thing print('this function is not working yet!') actSetCheck = input('Change username or password?') if(actSetCheck == 'user' or actSetCheck == 'User' or actSetCheck == 'Username' or actSetCheck == 'username'): yourNewUser = input('What would you like your new username to be?') elif(actSetCheck == 'pass' or actSetCheck == 'Pass' or actSetCheck == 'password' or actSetCheck == 'Password'): yourNewPass = input('What would you like your new password to be?') elif(dirCheck == 'n' or dirCheck == 'N'): request() #setting the variables early on yourNewUser == 0 yourNewPass == 0 #This is the "title screen" print('_____This is a password keeper_____') #checking if the user has an account actCheck = input('Do you already have an account?') if(actCheck == 'Yes' or actCheck == 'yes'): #Checking to see if the yourNewUser or yourNewPass variable has been changed if(yourNewUser != '0' or yourNewPass != '0'): changedpass() #asking for user's name and password else: yourUser = input('___What is your Username?___') yourPass = input('___What is your Password?___') if(yourUser == 'ari' and yourPass == 'rycbar1234'): dirCheck = input('Account settings?[y,n]') if(dirCheck == 'y' or dirCheck == 'Y'): #This is the start of the change password/username thing print('this function is not working yet!') actSetCheck = input('Change username or password?') if(actSetCheck == 'user' or actSetCheck == 'User' or actSetCheck == 'Username' or actSetCheck == 'username'): yourNewUser = input('What would you like your new username to be?') elif(actSetCheck == 'pass' or actSetCheck == 'Pass' or actSetCheck == 'password' or actSetCheck == 'Password'): yourNewPass = input('What would you like your new password to be?') elif(dirCheck == 'n' or dirCheck == 'N'): request() #incorrect password thing else: print('Incorrect Username or password')
原文:https://stackoverflow.com/questions/36414693
最满意答案
如果您使用RelativeLayout,则可以轻松地将其他视图对齐到屏幕的顶部/底部:
使用
android:layout_alignParentTop="true"
或者
android:layout_alignParentBottom="true"
左/右相同:
android:layout_alignParentLeft="true" or android:layout_alignParentRight="true"
那这个呢 ?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:text="@string/label_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"/> <TextView android:text="@string/label_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right" android:layout_alignParentTop="true" android:layout_alignParentRight="true"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello_world" android:gravity="center" android:textSize="40sp" android:layout_centerInParent="true"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/next_activity" android:gravity="center" android:textSize="30sp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true"/>
这是你在找什么?
If you use a RelativeLayout instead, you can align other views to the top/bottom of the screen easily:
use
android:layout_alignParentTop="true"
or
android:layout_alignParentBottom="true"
same for left/right :
android:layout_alignParentLeft="true" or android:layout_alignParentRight="true"
What about this ?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:text="@string/label_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"/> <TextView android:text="@string/label_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right" android:layout_alignParentTop="true" android:layout_alignParentRight="true"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello_world" android:gravity="center" android:textSize="40sp" android:layout_centerInParent="true"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/next_activity" android:gravity="center" android:textSize="30sp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true"/>
Is it what you are looking for ?
相关问答
更多-
找到了 ! 它是LinearLayout中的android:layout_gravity="center" ,是罪魁祸首。 只要删除这一切,一切都应该没问题。 Found it ! It's the android:layout_gravity="center" in the LinearLayout that is the culprit. Just delete this and everything should be fine.
-
中心布局Android(Center layout Android)[2022-12-09]
将包含按钮的LinearLayout设置为wrap_content的宽度,并使center_horizontal具有重力。只需给你的LinearLayout定位,休息一切都很完美