用户输入中的资本创建回溯错误?

时间:2017-08-15 14:28:58

标签: python python-3.x input lowercase

我有一段简单的代码,您输入一个名称以接收有关该人的更多信息。例如,你进入约翰'有关约翰的信息。但是,如果用户输入' John'或者' JOHN'例如,我得到以下回溯错误。

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    John
NameError: name 'John' is not defined

我已经能够.lower().lower()插入到我的代码中(目前已移除)而不会出现错误但是它仍然不接受John&#39; JOHN&#39; JOHN&#39;等等,并以小写形式返回有关John的所有信息 - 这不是我想要的。

是否可以允许用户使用大写字母输入?我是Python的新手并且非常欣赏这一点非常简单,我已经看到类似的帖子与input().lower()class Employees: def __init__(self, name, lastname, age, department): self.name = name self.lastname = lastname self.age = age self.department = department def displayEmployees(self): return("The employee is called " + self.name + ' ' + self.lastname + " and is " + str(self.age) + " years old, and works in the " + self.department + " department.") employee1 = Employees("Sam", "Smith", 25, "Office") employee2 = Employees("John", "Smith", 23, "Admin") employee3 = Employees("Amy", "Smith", 28, "Admin") employee4 = Employees("Alice", "Smith", 30, "Reception") employee5 = Employees("Chris", "Smith", 51, "Managers") sam = employee1.displayEmployees() john = employee2.displayEmployees() amy = employee3.displayEmployees() alice = employee4.displayEmployees() chris = employee5.displayEmployees() print("Please input the first name of the employee you wish to find out more about the employee") 回复和类似,但很难找到在我的代码中包含它的地方允许供用户输入&#39; John&#39;等等。谢谢。

public class PasswordKeeperActivity extends Activity implements 
AdapterView.OnItemSelectedListener {

// initialise
EditText username, password, note;
Button save, reset;
public String savedata = Environment.getExternalStorageDirectory().toString();

String[] countryNames={"Google", "Yahoo", "Facebook", "Twitter", "Instagram", "BBM", "Skype", "Other"};
int flags[] = {R.drawable.google, R.drawable.yahoo, R.drawable.facebook, R.drawable.twitter, R.drawable.instagram, R.drawable.bbm, R.drawable.skype, R.drawable.other};

private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
    Manifest.permission.READ_EXTERNAL_STORAGE,
    Manifest.permission.WRITE_EXTERNAL_STORAGE
  };
//  for inflating the menu
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}

// on selection of the menu
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.view_passwords:
    Intent intent = new Intent(this, PasswordView.class);
    startActivity(intent);
    return true;

default:
    return super.onOptionsItemSelected(item);
}
}

public void onItemSelected(AdapterView<?> parent, View view,
                       int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)

}

public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);

initialise();

//Getting the instance of Spinner and applying OnItemSelectedListener on 
it
Spinner spin = (Spinner) findViewById(R.id.planets_spinner);
spin.setOnItemSelectedListener(this);

CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), 
flags, countryNames);
spin.setAdapter(customAdapter);

//to set the site Edit Text to get the focus




  // save the data to the textfile
  save.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

        // creates hidden directory if not existing
        File dir = new File(getCacheDir(), "/sk/");
        if (!dir.exists()) {
            dir.mkdirs();
        }

        // saving data part
        String sFileName = getCacheDir() + "/sk/logp.csv";
        try {
            FileWriter writer = new FileWriter(sFileName, true);

            String  countryNames, sUser, sPass, sAdd;

            countryNames =
            sUser = username.getText().toString();
            sPass = password.getText().toString();
            sAdd = note.getText().toString();



            if ((sUser.equals("")) && (sPass.equals("")) && 
    (sAdd.equals(""))) {
                Toast.makeText(getBaseContext(), "Please Enter At least 
    one Field",
                        Toast.LENGTH_SHORT).show();

            } else {
                if (sUser.equals(""))
                    sUser = "null";
                if (sPass.equals(""))
                    sPass = "null";
                if (sAdd.equals(""))
                    sAdd = "null";


                // encrypting the passwords before saving
                SimpleCrypto mcrypt = new SimpleCrypto();
                sPass = SimpleCrypto.bytesToHex( mcrypt.encrypt(sPass) );
                //sPass = SimpleCrypto.encrypt("fugly", sPass);




                writer.append(sUser);
                writer.append(',');

                writer.append(sPass);
                writer.append(',');
                writer.append(sAdd);

                writer.append('\n');

                // generate whatever data you want

                writer.flush();
                writer.close();

                Toast.makeText(getBaseContext(), "Password Saved!",
                        Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(PasswordKeeperActivity.this, 
    PasswordView.class);
                String[] myStrings = new String[] {"Google", "Yahoo", 
   "Facebook", "Twitter", "Instagram", "BBM", "Skype", "Other"};
                int logo[]  = new int[] {R.drawable.google, 
   R.drawable.yahoo, R.drawable.facebook, R.drawable.twitter, 
   R.drawable.instagram, 
   R.drawable.bbm, R.drawable.skype, R.drawable.other};
                intent.putExtra("strings", myStrings);
                intent.putExtra("logos", logo);
                startActivity(intent);
            }

        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.getMessage(),
                    Toast.LENGTH_SHORT).show();
        }

    }
});

 // Reset
 reset.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

        countryNames.equals("Google");
        note.setText("");
        username.setText("");
        password.setText("");
        Toast.makeText(getBaseContext(), "Field(s) Cleared!",
                Toast.LENGTH_SHORT).show();
    }
});

}



public void initialise() {


username = (EditText) findViewById(R.id.input_name);
password = (EditText) findViewById(R.id.input_email);
note = (EditText) findViewById(R.id.input_password);

save = (Button) findViewById(R.id.buttonSave);
reset = (Button) findViewById(R.id.ButtonReset);
}

/**
 * Checks if the app has permission to write to device storage
 *
 * If the app does not has permission then the user will be prompted to 
 grant permissions
 *
  * @param activity
  */
 public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, 
Manifest.permission.WRITE_EXTERNAL_STORAGE);

 if (permission != PackageManager.PERMISSION_GRANTED) {
    // We don't have permission so prompt the user
    ActivityCompat.requestPermissions(
            activity,
            PERMISSIONS_STORAGE,
            REQUEST_EXTERNAL_STORAGE
    );
}
}
}

1 个答案:

答案 0 :(得分:1)

您需要使用:

name = input('Please enter the persons name').lower()