0

This is probably the most ugly code you'll ever see, anyway I need help, cause I didn't get it how loops works. The idea of the script is easy, you put a name of a Superhero and you'll get his real name. Then if you want, you can choose the "biography" section, to get into details.

import sys
import os
super_heros = {'Hulk': 'Bruce Banner',                # Creo lista supereroei
               'Capitan America': 'Steve Rogers',
               'Spiderman': 'Peter Parker'}

hero_biography = {'Bruce Banner': 'David Banner nasce in California. '
                                   'Si laurea con il massimo dei voti in medicina, radiologia, genetica'
                                   'e biologia molecolare. '
                                   'A pochi anni dalla laurea, acquisisce una grande fama come medico in malattie'
                                   'rare e genetiche, nonché come ricercatore in ambito radiologico, cellulare, e '
                                   'atomico. '
                                   'Per anni insegna scienze genetiche a Princeton e a Stanford, dove acquisisce'
                                   'una fama sempre crescente nel campo delle ricerche sugli impulsi emotivi e '
                                   'sui raggi gamma. '
                                   'Le sue ricerche subiscono una grande ispirazione quando legge i primi articoli '
                                   'del dottorRonald Pratt, secondo il quale è possibile ottenere poteri guaritori '
                                   'da una manipolazione genetica. '
                                   'Al termine insegnamento come docente universitario, Banner lavora presso un '
                                   'laboratorio dove è affiancato da Elèna Marks, una vecchia compagna del college',
                  'Peter Parker' : 'Prova'}

print('Script creato da Federico Di Lembo')

while True:  # Loop per condizione soddisfatta

    choice = input('Nome Supereroe:')
    if choice == 'Hulk':
        print(super_heros['Hulk'])
    elif choice == 'Capitan America':
        print(super_heros['Capitan America'])
    elif choice == 'Spiderman':
        print(super_heros['Spiderman'])

    elif choice == 'Esc':  # Imposto uscita dal programma
        sys.exit(0)
    elif choice == 'Biografia':  # Imposto uscita dal loop
        break
    else:
        choice == ''
        print('Nome inesistente')

while True:  # Imposto Nuovo Loop per la seconda scelta
    x = 1
    if x > 0:
        newchoice = input('Biografia: digitare nome reale del supereroe ==> ')
    if newchoice == 'Bruce Banner':
        print(hero_biography['Bruce Banner'])
        break
    elif newchoice == 'Peter Parker':
        print(hero_biography['Peter Parker'])
    if newchoice == '':
        newchoice


while True:
    x = 1
    if x > 0:
        secondnewchoice = input('Desideri continuare la ricerca nella sezione Biografia?')

    if secondnewchoice == 'No':
        break
    if secondnewchoice == 'Si':
        newchoice



os.execl(sys.executable, sys.executable, *sys.argv)

In the last part of the code, I don't understand how to "recall" the newchoice input. In the last loop the question is "Would you like to stay in the biography section?", then if your answer is "yes" "Si", I want to refer to newchoiche input, just above. What I'm doing wrong?

I'm sorry for my english.

1 Answer 1

1

while loops don't start a new scope. newchoice is still visible and set after the loop where it is set completes.

while True:
    foo = 3
    break
print foo   # Outputs 3        

Your problem appears to be that you aren't actually doing anything with newchoice in the last loop; it's a string-valued variable, not a function call. Perhaps you want a nested loop:

while True:  # Loop A
    while True:  # Loop B
        newchoice = input('Biografia: digitare nome reale del supereroe ==> ')
        if newchoice == '':
            break  # Exit loop B, moving on to loop C
        elif newchoice not in hero_biography:
            continue
        else:
            print(hero_biography[newchoice])

    while True:  # Loop C
        stay = input('Desideri ...')
        if stay in ("No", "Si"):
            break  # Exit loop C
    if stay == "No":
        break  # Exit loop A
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.