Home > Software design >  I need help witht this python code its being annoying it gives me a error
I need help witht this python code its being annoying it gives me a error

Time:11-06

Code :

from sys import exit
didlogcorrect = 1
Minecraft = 'Minecraft'
Roblox = 'Roblox'
Exit = 'Exit'
chrome = 'chrome'
  print('Welcome To PotatOS')
  user_input = input("What would you like to do on PotatOS: ")
  if user_input == Roblox:
   print('Unable to connect to wifi Now terminating process')
  elif user_input == Exit:
    exit()
  elif user_input == chrome:
    print('You open Chrome Thinking What should i do now Maybe youtube idk, Then you close Chrome cause your bored!')
  elif user_input == Minecraft:
    print('Crashed, Reason 1, you close it cause your mad it crashed')
else:
  print('PotatOS : BEEERGZ Unable to understand')

crash :

Welcome To PotatOS
What would you like to do on PotatOS: Minecraft
Traceback (most recent call last):
  File "/home/Username/Coding/PotatOS-1.0.1.txt", line 27, in <module>
    if user_input == Roblox:
NameError: name 'Roblox' is not defined

Please help I would like someone with python knowledge to look at this I still cant find out why this wont work =(

I tried Multiple things but it still crashed, Sadly.

Thanks for being here and reading if you can help please let me know or help

CodePudding user response:

The way you set up your code does not check against the values inside your variables:

Roblox = 'Roblox'
Exit = 'Exit'
Minecraft = 'Minecraft'
chrome = 'chrome'
Password = 'Test'
Username = 'BTP'

The built-in input function writes a prompt to standard output without a trailing newline and then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

By setting maxsplit=1 in your split method you limit split to only split at the first comma found and no more. You can solve this by not setting maxsplit

# Make sure to enter 4 comma separated values
chrome, Roblox, Exit, Minecraft = input("What would you like to do on PotatOS").split(",")

Testing string with maxsplit=1:

>>> "abc,def,ghi,jkl".split(",",1)
['abc', 'def,ghi,jkl']
>>> # maxsplit not set
>>> "abc,def,ghi,jkl".split(",")
['abc', 'def', 'ghi', 'jkl']

As you want to compare values, you need to check the user input like this using if/elif/else statement:

# Expects an answer like Roblox, Exit, chrome
user_input = input("What would you like to do on PotatOS: ")
if user_input == Roblox:
    # do Roblox stuff
elif user_input == Exit:
    # do Exit stuff
elif user_input == chrome:
    # do chrome stuff
else:
    # do stuff not saved in variable

Just replace the comments with your action.

CodePudding user response:

This error is coming because you're trying to map 4 values (chrome, Roblox, Exit, and Minecraft) while giving only one value from input().

If you want to put 4 different values in one input with space and be mapped into each variables,
e.g.

>>> What would you like to do on PotatOS
>>> Here,Type,Your,Answer

--
result:
chrome -> 'Here'
Roblox -> 'Types'
Exit -> 'Your'
Minecraft -> 'Answer'

You can try adding .split() followed by input()

So your code might look like this.
Try to remove the split count value first, and make sure you're doing right in typing in input values.

chrome, Roblox, Exit, Minecraft = input("What would you like to do on PotatOS").split(",")

CodePudding user response:

I suggest doing

user_input = input("What would you like to do on PotatOS")

Then doing something with user_input. The key here is to keep each line of code as simple as possible. As you learn more about python, you will discover when it is appropriate to chain two method calls together, but until then, don't.

  • Related