Jump to content

Wikipedia:Reference desk/Archives/Computing/2020 January 25

From Wikipedia, the free encyclopedia
Computing desk
< January 24 << Dec | January | Feb >> Current desk >
Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is a transcluded archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


January 25[edit]

Ubnutu 18.04.3 LTS[edit]

In Virtual Box, how can I expand the disc space? --Thegooduser Life Begins With a Smile :) 🍁 01:23, 25 January 2020 (UTC)[reply]

Thegooduser, more information is necessary. The nature of the guest VM will come into play here, as regards partitioning and provisioning extra volumes from the virtual media that VirtualBox can provide. There is more than one way to skin a cat here. You may be able to expand the virtual media (haven't checked, may depend on the volume type you specified at VM creation time.) The easiest way is just to add another virtual media volume that will be seen as a new connected physical disk, then format and partition it according to the guest's needs. And of course, you could always take advantage of shared folders and network storage, that will expand the available space to the VM while sharing it with the host, other guests, or other devices on your network. Elizium23 (talk) 23:19, 25 January 2020 (UTC)[reply]

What's wrong with this code?[edit]

For the record, this code is for a rock-paper-scissors game:

inp = "Yes"

while inp == "Yes" or "yes":
   move1 = input("What move would you like to make?: ")
   move2 = input("What move would you like to make?: ")
   if move1 == "Rock" or "rock":
       if move2 == "Rock" or "rock":
           print("It's a draw!")
       elif move2 == "Paper" or "paper":
           print(move2,"beats",move1,"!")
       elif move2 == "Scissors" or "scissors":
           print(move1,"beats",move2,"!")
   elif move1 == "Paper" or "paper":
       if move2 == "Rock" or "rock":
           print(move1,"beats",move2,"!")
       elif move2 == "Paper" or "paper":
           print("It's a draw!")
       elif move2 == "Scissors" or "scissors":
           print(move2,"beats",move1,"!")
   elif move1 == "Scissors" or "scissors":
       if move2 == "Rock" or "rock":
           print(move2,"beats",move1,"!")
       elif move2 == "Paper" or "paper":
           print(move1,"beats",move2,"!")
       elif move2 == "Scissors" or "scissors":
           print("It's a draw!")
   inp = input("Would you like to play again? If so, type either 'Yes' or 'yes': ")

Futurist110 (talk) 01:34, 25 January 2020 (UTC)[reply]

I don't know whether this is actual code for a specific language. In most programming languages, indentation is ignored and the two "elif move1 == ..." will be interpreted as elif to the immediately preceding elif and not to something earlier with the same indentation. If your language is like that then it probably has an endif or similar to end the closest unterminated if. PrimeHunter (talk) 02:40, 25 January 2020 (UTC)[reply]
It's probably true that in most programming languages indentation is ignored -- but not in Python, which this code appears to be.
The obvious mistake I see is in the second line,
while inp == "Yes" or "yes":
which should be
while inp == "Yes" or inp == "yes":
Hope this helps! --Trovatore (talk) 05:01, 25 January 2020 (UTC)[reply]
You could alternatively say "while inp in ['yes','Yes']", but preferably: "while inp.lower() == 'yes'". 2601:648:8202:96B0:0:0:0:4FFF (talk) 06:42, 25 January 2020 (UTC)[reply]
All lines containg or suffer from this problem. If you set move1 = input("What move would you like to make?: ").lower() you can simply test whether move1 == "rock".  --Lambiam 18:53, 25 January 2020 (UTC)[reply]
I should thank Futurist110 for this question, as I learned something interesting about Python's or operator from it.
The top-level takeaway is that bool("yes") evaluates as True, and therefore the while loop will never terminate (and similarly, the first branch of each of the succeeding conditionals will always be taken).
But at a deeper level of detail, A or B evaluates as A if bool(A) is True, and otherwise as B.
So the while condition, namely inp == "Yes" or "yes", evaluates as True if the user typed "Yes" — and otherwise it evaluates as "yes".
I expect this is meant to enable assignment statements with a fallback. What constitutes a "pythonic" usage of this mechanism I'm not really sure. I don't bellyfeel pythonicity yet. --Trovatore (talk) 23:12, 25 January 2020 (UTC)[reply]
Off topic comment The only programming language I know fluently is Java, and all I can say is, wow. Is this what all other languages are like, or does Python just have ridiculously simple syntax? --PuzzledvegetableIs it teatime already? 03:03, 26 January 2020 (UTC)[reply]
Puzzledvegetable, Python just has simple syntax.I'll return with an example in Rust in a minute moonythedwarf (Braden N.) 03:25, 26 January 2020 (UTC)[reply]
Example on Puzzledvegetable's talk page. moonythedwarf (Braden N.) 03:56, 26 January 2020 (UTC)[reply]
Python is simple because indentation matters. Therefore, teaching Python is a pain. Every lab results in repeating over and over and over: "Check your indentation." 135.84.167.41 (talk) 13:34, 28 January 2020 (UTC)[reply]
Here is my Haskell version 2601:648:8202:96B0:0:0:0:4FFF (talk) 10:28, 26 January 2020 (UTC)[reply]
data Move = Rock | Paper | Scissors deriving (Show, Eq)
data Result = Win | Lose | Draw deriving (Show, Eq)

opposite :: Result -> Result
opposite result = case result of
  Win -> Lose
  Lose -> Win
  Draw -> Draw

play :: Move -> Move -> Result
play m1 m2 = case (m1,m2) of
  (Rock,Scissors) -> Win
  (Scissors,Paper) -> Win
  (Paper,Rock) -> Win
  _ | m1 == m2 -> Draw
    | otherwise -> opposite (play m2 m1)
If you're interested in what other languages look like this is worth a browse. don't worry about the ".de" it's in English. Martin of Sheffield (talk) 11:14, 26 January 2020 (UTC)[reply]
Martin of Sheffield, Can't forget Rosetta Code --moonythedwarf (Braden N.) 14:21, 28 January 2020 (UTC)[reply]
Moonythedwarf, Agreed, it's another good site, though possibly a bit advanced for this context. One nice use of the hello world site is to set up a test suite to see what languages are usable on a particular machine. It acts as a nice check that a cluster is doing what is expected. Martin of Sheffield (talk) 15:01, 28 January 2020 (UTC)[reply]