输入错误的用户输入时,设置循环以继续编程很困难

时间:2018-02-17 17:36:50

标签: python

我是编程和Python的新手,我正在努力设置循环以在用户输入不正确时继续使用这个基本名称生成器。我尝试研究这个问题,但通常使用整数等找到答案,并且难以翻译示例来执行我​​需要程序执行的操作。我不太确定我使用IF,ELSE和ELIF语句是否正确使用。感谢任何能够就此主题提供指导的人。

void main() {
  *((volatile unsigned char *)(0x27)) = 128;
  volatile unsigned char * x = (unsigned char *) 300;
  volatile unsigned char * y = (unsigned char *) 301;
  volatile unsigned char * z = (unsigned char *) 302;
start:
  *((volatile unsigned char *)(0x28)) = 0;
  for (*x = 0; *x < 255; *x++) {
    for (*y = 0; *y < 255; *y++) {
      for (*z = 0; *z < 255; *z++) {
      }
    }
  }
  *((volatile unsigned char *)(0x28)) = 128;
  for (*x = 0; *x < 255; *x++) {
    for (*y = 0; *y < 255; *y++) {
      for (*z = 0; *z < 255; *z++) {
      }
    }
  }
  goto start;
}

2 个答案:

答案 0 :(得分:1)

如果使用python 2,则必须使用raw_input

无论如何,如果我得到你想做的事情,这看起来应该是基本的主要启动程序:

import random

if __name__ == '__main__':
    print("Hello, world")

    while 1:
        sex = raw_input("What's your sex? ")

        # If user chooses Male the program will execute.
        if sex == 'Male':

            # This is where the list of male first names are stored.
            firstNameMale = ['Ughash', 'Spoguk', 'Truigig', 'Wudhagh', 'Wumkbanok', 'Hogug', 'Karrhig', 'Pargu',
                             'Xokuk', 'Furbog']

            # This is where the list of male last names are stored.
            lastNameMale = ['Opkagut', 'Zombilge', 'Grushnag', 'Gujarek', 'Igmut', 'Gorgo', 'Filge', 'XorokuShamob',
                            'Zurgha']
            # This is where the random function calls on the lists to assemble the name.
            firstName = random.choice(firstNameMale)
            lastName = random.choice(lastNameMale)
            print('Your name is ' + firstName + ' ' + lastName)
            break

        # If user chooses Female the program will execute.
        elif sex == "Female":
            # This is where the list of female first names are stored.
            firstNameFemale = ['Ughash', 'Spoguk', 'Truigig', 'Wudhagh', 'Wumkbanok', 'Hogug', 'Karrhig', 'Pargu',
                               'Xokuk',
                               'Furbog']
            # This is where the list of female last names are stored.
            lastNameFemale = ['Opkagut', 'Zombilge', 'Grushnag', 'Gujarek', 'Igmut', 'Gorgo', 'Filge', 'XorokuShamob',
                              'Zurgha']
            # This is where the random function calls on the lists to assemble the name.
            firstName = random.choice(firstNameFemale)
            lastName = random.choice(lastNameFemale)
            print('Your name is ' + firstName + ' ' + lastName)
            break

        # If user did not choose Male of Female program will execute.
        else:
            print('Please choose from Male or Female.  Remember capitalization counts!')

在这里我做了一个无限循环,直到你输入男性或女性。希望它有所帮助。

答案 1 :(得分:0)

也许像下面这样的结构会对你有所帮助:

def get_sex():
    sex = ''
    while sex is not 'Male' and sex is not 'Female':
          sex = input("What's your sex?")
    return sex

def get_name(sex):
    if sex is 'Male':
         name = ...
    else if sex is 'Female':
         name = ...
    return name

sex = get_sex()
name = get_name(sex)
print(name)

通过拆分代码的逻辑并使用while语句继续查询,直到用户输入有效选项,我们改进了代码流。