要求输入但不承认

时间:2016-04-02 15:13:01

标签: python input

当您运行程序并选择某些内容时,它有时不会确认输入。对不起,如果有明显的错误。我还是很陌生。感谢您提前获得所有帮助。 这不是一个主要的问题,但它是一个学校的事情,所以最好我想解决它。

import webbrowser as web
import random as rand
print('1: Website Listings')
print('2: Password Generator')
print('3: Number Guessing Game')
print('4: Calculator')
while True:
    user_input1 = str(input(': '))
    print('')

#Website Listings:

    if user_input1 == '1':  
        print('1: Google')
        print('2: Youtube')
        print('3: 9GAG')
        print('4: CanYouRunIt')

        user_input2 = str(input(': '))

        if user_input2 == '1':
            web.open('https://www.google.co.za/')
            print('')
            print('1: Website Listings')
            print('2: Password Generator')
            print('3: Number Guessing Game')
            print('4: Calculator')
            user_input1 = str(input(': '))

        if user_input2 == '2':
            web.open('https://www.youtube.com/?gl=ZA')
            print('')
            print('1: Website Listings')
            print('2: Password Generator')
            print('3: Number Guessing Game')
            print('4: Calculator')
            user_input1 = str(input(': '))

        if user_input2 == '3':
            web.open('http://9gag.com/')
            print('')
            print('1: Website Listings')
            print('2: Password Generator')
            print('3: Number Guessing Game')
            print('4: Calculator')
            user_input1 = str(input(': '))


        if user_input2 == '4':
            web.open('http://www.systemrequirementslab.com/cyri')
            print('')
            print('1: Website Listings')
            print('2: Password Generator')
            print('3: Number Guessing Game')
            print('4: Calculator')
            user_input1 = str(input(': '))

#Password Generator:

    if user_input1 == '2':
        total_digits = 0
        max_digits = int(input('How many digits? '))
        while total_digits != max_digits:
            print(rand.randint(0,9))
            total_digits+=1
        print('')
        print('1: Website Listings')
        print('2: Password Generator')
        print('3: Number Guessing Game')
        print('4: Calculator')
        user_input1 = str(input(': '))

#Number Guessing Game:
    if user_input1 == '3':
        print('')
        lower_value = int(input('Lowest Number: '))
        print(' ')
        higher_value = int(input('Highest Number: '))
        print(' ')
        tries = int(input('Amount of tries before failure: '))
        print(' ')
        answer = rand.randint(lower_value, higher_value)

        while tries != 0:
            guess = int(input('Your guess: '))
            if guess > answer:
                print('The answer is smaller')
                tries-=1
                print('You have ' + str(tries) + ' try(ies)' + ' left')
                print(' ')
            elif guess < answer:
                print('The answer is larger')
                tries-=1
                print('You have ' + str(tries) + ' try3(ies)' + ' left')
                print(' ')
            elif guess == answer:
                print('!!! YOU WON !!!')
                print('You had ' + str(tries) + ' try(ies)' + ' left')
                print(' ')
                break
            if tries == 0:
                print('!!! YOU LOSE !!!')
                print(' ')
                break
        print('')
        print('1: Website Listings')
        print('2: Password Generator')
        print('3: Number Guessing Game')
        print('4: Calculator')
        user_input1 = str(input(': '))

#Calculator:

    print('')    
    if user_input1 == '4':
        print('These are your options: ')
        print('Type "1" to add two numbers')
        print('Type "2" to subtract two numbers')
        print('Type "3" to multiply two numbers')
        print('Type "4" to divide two numbers')
        print('Type "5" to determine the product of an exponent')
        print('Type "6" to return to main screen')
        print('')

        user_input3 = input('What do you want to do? ')

        if user_input3 == '6':
            print('')
            print('1: Website Listings')
            print('2: Password Generator')
            print('3: Number Guessing Game')
            print('4: Calculator')
            user_input1 = str(input(': '))

        elif user_input3 == '1':
            num1 = float(input('Please enter the first number: '))
            num2 = float(input('Please enter a second number to add: '))
            result=str(num1 + num2)
            print('The answer is: ' + result)
            print('')
            print('1: Website Listings')
            print('2: Password Generator')
            print('3: Number Guessing Game')
            print('4: Calculator')
            user_input1 = str(input(': '))

        elif user_input3 == '2':
            num1 = float(input('Please enter the first number: '))
            num2 = float(input('Please enter a second number to subtract: '))
            result=str(num1 - num2)
            print('The answer is: ' + result)
            print('')
            print('1: Website Listings')
            print('2: Password Generator')
            print('3: Number Guessing Game')
            print('4: Calculator')
            user_input1 = str(input(': '))

        elif user_input3 == '3':
            num1 = float(input('Please enter the first number: '))
            num2 = float(input(' Please enter a second number to multiply: '))
            result = str(num1 * num2)
            print('The answer is: ' + result)
            print('')
            print('1: Website Listings')
            print('2: Password Generator')
            print('3: Number Guessing Game')
            print('4: Calculator')
            user_input1 = str(input(': '))

        elif user_input3 == '4':
            num1 = float(input('Please enter the first number '))
            num2 = float(input('Please enter a second number to divide by: '))
            result = str(num1 / num2)
            print('The answer is: ' + result)
            print('')
            print('1: Website Listings')
            print('2: Password Generator')
            print('3: Number Guessing Game')
            print('4: Calculator')
            user_input1 = str(input(': '))

        elif user_input3 == '5':
            num1 = float(input('Please enter the base number: '))
            num2 = float(input('Please enter the exponent: '))
            result = str(num1 ** num2)
            print('The answer is: ' + result)
            print('')
            print('1: Website Listings')
            print('2: Password Generator')
            print('3: Number Guessing Game')
            print('4: Calculator')
            user_input1 = str(input(': '))

        else:
            print('Unknown command')
            print('')
            print('1: Website Listings')
            print('2: Password Generator')
            print('3: Number Guessing Game')
            print('4: Calculator')
            user_input1 = str(input(': '))

2 个答案:

答案 0 :(得分:0)

在所有第一个if语句的末尾,添加一个传递。

答案 1 :(得分:-1)

input已经返回一个字符串,因此使用str再次将其转换为字符串是没有意义的。由于您的输入将是一个数字,请将其转换为整数

# ... rest of your code

inp = input("Enter a numerical option: ")
try:
    inp = int(inp)
except:
    print(inp, "is not a valid option")
    continue   # this will skip everything below and go to the next instance of the while loop which will ask the user again for input

# ... rest of your code

关于代码的结构,可以将其拆分为可以调用的较小函数。

def webListings():
    print('1: Google')
    ...

def passGen()
    print(...)
    ...

然后你可以做这样的事情

if user_input1 == 1:
    webListings()
else if user_input1 == 2:
    passGen()