2016-04-02 18 views
-1

Wenn Sie das Programm ausführen und etwas auswählen, wird die Eingabe manchmal nicht bestätigt. Entschuldigung, wenn ein offensichtlicher Fehler vorliegt. Ich bin noch ziemlich neu dabei. Danke für die Hilfe im Voraus. Dies ist kein großes Problem, aber es ist für eine Schule Sache, also würde ich es am liebsten reparieren.Fragt nach Eingabe, quittiert aber nicht

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(': ')) 

Antwort

-1

input gibt eine Zeichenfolge bereits, so gibt es keinen Punkt in einen String wieder mit str in Gießen. Da Ihre Eingabe eine Zahl sein wird, wandelt es in eine ganze Zahl anstelle

# ... 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 

In Bezug auf die Struktur des Codes kann es aufgeteilt in kleinere Funktionen, die stattdessen aufgerufen werden kann.

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

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

Und dann können Sie etwas tun, wie diese

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

am Ende aller ersten if-Anweisungen, einen Pass hinzuzufügen.

Verwandte Themen